Mocked method returns null
I have a simple test method in my Silverlight project:
[TestMethod]
[Tag("User")]
public void ViewModel_NewUserAdded_DefaultCulturesLoaded()
{
//setup
Mock<INameService> mockNameService = new Mock<INameService>();
MainViewModel viewModel = new MainViewModel();
mockNameService
.Setup(m => m.DefaultCultures(It.IsAny<Action<LoadOperation<kk_mp_name>>>()))
.Returns(new Mock<OperationBase>(null).Object);
viewModel.ContextName = mockNameService.Object;
//action
Messenger.Default.Send(-1, "New User Added");
//verify
mockNameService.Verify(
(mo) => mo.DefaultCultures(It.IsAny<Action<LoadOperation<kk_mp_name>>>()),
Times.Exactly(1));
}
There DefaultCultures method always returns null instea开发者_JAVA百科d of new mock-object. What I doing wrong?
From your comments, it looks like
var mockNameService = new Mock<INameService>(MockBehavior.Strict);
didn't help much.
So my next point of query would be to ask why you're passing in Null here.
new Mock<OperationBase>(null).Object
Can you post the code for OperationBase ?
Is Messenger.Default.Send
using the mock object you set on viewModel.ContextName
? If you step into the code to the point where you expect the DefaultCultures
to be invoked, you can check the concrete type of ContextName
to ensure it is the mock instance.
I also find it helpful to add a .Callback
on the mock set up, on which to set a breakpoint and ensure that the method is being invoked:
MainViewModel viewModel = new MainViewModel();
mockNameService
.Setup(m => m.DefaultCultures(It.IsAny<Action<LoadOperation<kk_mp_name>>>()))
.Callback((Action<LoadOperation<kk_mp_name>> a) =>
{
; // Set breakpoint here
})
.Returns(new Mock<OperationBase>(null).Object);
精彩评论