Last active
December 17, 2018 18:22
-
-
Save ChrisMoney/20b09478cd5f7167f5ea89068117bbbb to your computer and use it in GitHub Desktop.
Invoke Method - Unit Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace PFMGoalsNotificationTests | |
{ | |
[TestClass] | |
public class GoalNotificationServiceTests | |
{ | |
private Mock<ILogger> _mockLogger; | |
private Mock<IFinlockerDataProvider> _dataProvider; | |
private Mock<INotificationService> _mockNotificationService; | |
private Mock<IGoalService> _mockGoalService; | |
private Mock<IGoalSnapshotService> _mockSnapshotService; | |
private Mock<INotificationCategorySettingService> _mockNotificationCategorySettingService; | |
private Mock<IFinlockerDbContext> _dbContext; | |
private IGoalNotificationService _target; | |
[TestInitialize] | |
public void Init() | |
{ | |
DataCrypto.SetSecret("TestSecret"); | |
_mockLogger = new Mock<ILogger>(); | |
_dataProvider = new Mock<IFinlockerDataProvider>(); | |
_mockNotificationService = new Mock<INotificationService>(); | |
_mockGoalService = new Mock<IGoalService>(); | |
_mockNotificationCategorySettingService = new Mock<INotificationCategorySettingService>(); | |
_mockSnapshotService = new Mock<IGoalSnapshotService>(); | |
_dbContext = new Mock<IFinlockerDbContext>(); | |
_target = new GoalNotificationService( | |
_mockLogger.Object, | |
_dataProvider.Object, | |
_mockNotificationService.Object, | |
_mockGoalService.Object, | |
_mockNotificationCategorySettingService.Object, | |
_mockSnapshotService.Object | |
); | |
} | |
private IGoalNotificationService GoalService() | |
{ | |
return new GoalNotificationService( | |
_mockLogger.Object, | |
_dataProvider.Object, | |
_mockNotificationService.Object, | |
_mockGoalService.Object, | |
_mockNotificationCategorySettingService.Object, | |
_mockSnapshotService.Object | |
); | |
} | |
[TestMethod] | |
public void SendGoalNotificationTest() | |
{ | |
// Mock & Setup GoalNotificationService | |
Mock<IGoalNotificationService> mockGoalService = new Mock<IGoalNotificationService>(); | |
mockGoalService.Setup(mock => mock.SendGoalNotificationsAsync()); | |
// Invoke SendGoalNotificationAsync | |
Task test = mockGoalService.Object.SendGoalNotificationsAsync(); | |
// Verify service is not null | |
Assert.IsNotNull(mockGoalService.Object); | |
// Verify method was called | |
mockGoalService.Verify(mock => mock.SendGoalNotificationsAsync(), Times.AtLeastOnce); | |
// setup data provider object | |
_dataProvider.Setup(x => x.CreateFinlocker()).Returns(_dbContext.Object); | |
// instantiate mock goal model | |
var mockDbSetGoal = new Mock<DbSet<FinLocker.DAL.Goal>>(); | |
// add goal to mock dbset | |
mockDbSetGoal.Setup(g => g.Add(It.IsAny<FinLocker.DAL.Goal>())).Returns(new FinLocker.DAL.Goal()); | |
_dbContext.Setup(x => x.Goals).Returns(mockDbSetGoal.Object); | |
// instantiate mock snapshot model | |
var mockDbSetGoalSnapshot = new Mock<DbSet<Goal_Snapshot>>(); | |
// add goal snapshot to mock dbset | |
mockDbSetGoalSnapshot.Setup(g => g.Add(It.IsAny<Goal_Snapshot>())).Returns(new FinLocker.DAL.Goal_Snapshot()); | |
_dbContext.Setup(x => x.Goal_Snapshot).Returns(mockDbSetGoalSnapshot.Object); | |
var goalService = GoalService(); | |
// invoke goal notification service | |
var response = goalService.SendGoalNotificationsAsync(); | |
// assert service returns an integer | |
Assert.IsInstanceOfType(response, typeof(Task<int>)); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment