Skip to content

Instantly share code, notes, and snippets.

@melvinlee
Last active September 17, 2015 02:20
Show Gist options
  • Save melvinlee/5f361180fb531221f8fb to your computer and use it in GitHub Desktop.
Save melvinlee/5f361180fb531221f8fb to your computer and use it in GitHub Desktop.
Unit test with moq framework
[Test]
public void Run_With_InboxMessages_Should_Call_Repo_Save()
{
//Arrange
var apiConfig = new ApiConfig();
apiConfig.SetCommPort(new CommPort());
var fakeApi = A.Fake<ISmsApi>();
var fakeLogging = A.Fake<ILogging>();
var fakeOutgoingsmsRepo = A.Fake<ISmsOutgoingRepository>();
var fakeIncomingsmsRepo = A.Fake<ISmsIncomingRepository>();
var inbox = new List<InboxMessage> {new InboxMessage()};
//Act
A.CallTo(() => fakeApi.GetInboxMessages(A<Action<ICollection<InboxMessage>, SmsServiceException>>.Ignored))
.Invokes(call =>
{
var action = ((Action<ICollection<InboxMessage>, SmsServiceException>) call.Arguments.First());
action(inbox, null);
});
A.CallTo(() => fakeApi.ModemReady).Returns(true);
A.CallTo(() => fakeApi.IsModemReady(A<Action<bool, string>>.Ignored)).Invokes(call =>
{
var action = ((Action<bool, string>) call.Arguments.First());
action(true, "");
});
var smsServer = new SmsServer(fakeApi, apiConfig, fakeOutgoingsmsRepo, fakeIncomingsmsRepo, fakeLogging);
smsServer.Run();
//Assert
A.CallTo(() => fakeApi.IsModemReady(A<Action<bool, string>>.Ignored)).MustHaveHappened();
A.CallTo(() => fakeIncomingsmsRepo.Add(A<IncomingSms>.Ignored)).MustHaveHappened();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment