Skip to content

Commit

Permalink
MAS-121 Clean up and add mailcontroller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d-lan1 committed Jan 15, 2020
1 parent 1b4a388 commit fb9a9cc
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 198 deletions.
154 changes: 154 additions & 0 deletions lambda/MAS.Tests/IntergrationTests/DailyEmailTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using MailChimp.Net.Core;
using MailChimp.Net.Interfaces;
using MailChimp.Net.Models;
using MAS.Models;
using MAS.Services;
using MAS.Tests.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using Xunit;
using Source = MAS.Models.Source;

namespace MAS.Tests.IntergrationTests
{
public class FakeController : Microsoft.AspNetCore.Mvc.Controller
{

}

public class DailyEmailTests : TestBase
{
IMailService MailService;

public DailyEmailTests(IMailService mailService)
{

MailService = mailService;
}

public override void Dispose()
{

}

Item exampleItem = new Item()
{
Id = "123",
Title = "Some Title",
Slug = "abc",
ShortSummary = "Some short summary",
ResourceLinks = "",
Comment = "",
Specialities = new List<Speciality>
{
new Speciality()
{
Key = "1a",
Title = "Some speciality",

}
},
EvidenceType = new EvidenceType()
{
Key = "1b",
Title = "Some evidence type"
},
Source = new Source()
{
Id = "1c",
Title = "Some source"
}
};
Item exampleItem2 = new Item()
{
Id = "123",
Title = "Some Title",
Slug = "abc",
ShortSummary = "Some short summary",
ResourceLinks = "",
Comment = "",
Specialities = new List<Speciality>
{
new Speciality()
{
Key = "1a",
Title = "Some speciality 2",

}
},
EvidenceType = new EvidenceType()
{
Key = "1c",
Title = "Some evidence type 2"
},
Source = new Source()
{
Id = "1c",
Title = "Some source"
}
};

//[Fact]
//public void CanCreateSingleItemEmail()
//{

// var items = new List<Item> { exampleItem };
// var actualHtml = this.MailService.CreateDailyEmailBody(items, mockController.Object);

// actualHtml.ShouldMatchApproved();

//}

//[Fact]
//public void CanCreateEmailWithTwoItemsSharingEvidenceType()
//{
// var items = new List<Item> { exampleItem, exampleItem };
// var actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());

// actualHtml.ShouldMatchApproved();

//}

//[Fact]
//public void CanCreateEmailWithTwoItemsDifferentEvidenceType()
//{
// var items = new List<Item> { exampleItem, exampleItem2 };
// var actualHtml = "";
// try
// {
// actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());
// }
// catch(Exception e)
// {
// var p = e.Message;
// }


// actualHtml.ShouldMatchApproved();

//}

//[Fact]
//public void ItemsWithManySpecialitiesRenderCorrectly()
//{
// exampleItem.Specialities.Add(new Speciality() { Key = "abcd", Title = "Another speciality" });
// var items = new List<Item> { exampleItem };
// var actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());

// actualHtml.ShouldMatchApproved();

//}

}
}
63 changes: 63 additions & 0 deletions lambda/MAS.Tests/UnitTests/Controllers/MailControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using MAS.Controllers;
using MAS.Models;
using MAS.Services;
using Microsoft.Extensions.Logging;
using Moq;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace MAS.Tests.UnitTests.Controllers
{
public class MailControllerTests
{
[Fact]
public async void GetDailyItemsForToday()
{

var mockContentService = new Mock<IContentService>();
var mailService = new MailController(Mock.Of<IMailService>(), mockContentService.Object, Mock.Of<IViewRenderer>(), Mock.Of<ILogger<MailService>>());

//Act
await mailService.PutMailAsync();

//Assert
mockContentService.Verify(mock => mock.GetDailyItemsAsync(null), Times.Once());
}

[Fact]
public async void GetDailyItemsForSpecificDate()
{
DateTime date = new DateTime(2020, 1, 15);
var mockContentService = new Mock<IContentService>();
var mailService = new MailController(Mock.Of<IMailService>(), mockContentService.Object, Mock.Of<IViewRenderer>(), Mock.Of<ILogger<MailService>>());

//Act
await mailService.PutMailAsync(date);

//Assert
mockContentService.Verify(mock => mock.GetDailyItemsAsync(date), Times.Once());
}

[Fact]
public async void RendersDailyViewWithContentItems()
{
var items = new List<Item>() { }.AsEnumerable();
var mockContentService = new Mock<IContentService>();
mockContentService.Setup(x => x.GetDailyItemsAsync(null)).ReturnsAsync(items);

var mockViewRenderer = new Mock<IViewRenderer>();

var mailController = new MailController(Mock.Of<IMailService>(), mockContentService.Object, mockViewRenderer.Object, Mock.Of<ILogger<MailService>>());

//Act
await mailController.PutMailAsync();

//Assert
mockViewRenderer.Verify(mock => mock.RenderViewAsync(mailController, "~/Views/MAD.cshtml", items, false), Times.Once());
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

142 changes: 0 additions & 142 deletions lambda/MAS.Tests/UnitTests/DailyEmailTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void CreateCampaignAndSendToMailChimp()
var mailService = new MailService(mockMailChimpManager.Object, mockLogger.Object);

//Act
var response = mailService.CreateAndSendCampaignAsync("Test Subject", "Preview Text", "Body Text");
var response = mailService.CreateAndSendDailyAsync("Test Subject", "Preview Text", "Body Text");

//Assert
response.Exception.ShouldBe(null);
Expand All @@ -51,7 +51,7 @@ public void ErrorInSendingCampaignShouldThrowError()
var mailService = new MailService(mockMailChimpManager.Object, mockLogger.Object);

//Act + Assert
Should.Throw<Exception>(() => mailService.CreateAndSendCampaignAsync("Test Subject", "Preview Text", "Body Text"));
Should.Throw<Exception>(() => mailService.CreateAndSendDailyAsync("Test Subject", "Preview Text", "Body Text"));
}
}
}
Loading

2 comments on commit fb9a9cc

@NICE-TeamCity
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity MAS / MAS Build 1.1.0.555-MAS-129_121__112_Dai outcome was FAILURE
Summary: Artifacts size 172 KB is 95% different from 3.6 MB in build #1.1.0.554-rD5C282E Build time: 00:00:41

@NICE-TeamCity
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity MAS / MAS Build 1.1.0.556-MAS-129_121__112_Dai outcome was FAILURE
Summary: Artifacts size 172 KB is 95% different from 3.6 MB in build #1.1.0.554-rD5C282E Build time: 00:00:35

Please sign in to comment.