开发者

Not sure how I Mock this simple Save(User user) method in my .NET code

I'm still trying to get my head around mocking. Right now, I'm trying to test my Save method on my UserService. As such, I'm mocking out my IRepository which my UserService class uses.

What I don't get is... if i normally save this user to my DB/Repository, it magically gets an Identity, which it then gets set, into my instance object. No rocket science stuff, here.

What I don't understand is, how do i mock this? should I care? I thought I should开发者_StackOverflow. Or.. is it that i don't care about that .. because I'm just making sure that the Repository method is called .. not so much that I get the correct data BACK from it.

Here's my pseudo unit test code. (unit test, not integration test .. hence the mock'd repository)...

[TestMethod]
public void GivenANewUserWithValidData_Save_ReturnsTheSameNewUserWithAUserIdDetermined()
{
    // Arrange.
    var const string passwordSalt = "V4BXAhmHq8IMvR7K20TgoQ=="
    var user = new User 
                   {
                       DisplayName = "Test",
                       Email = "foo@foo.com",
                       PasswordSalt = passwordSalt ,
                       Password = "foobar".ToSha2Hash(passwordSalt)
                   };

    var mockUserRepository = new Mock<IRepository<User>>();
    mockUserRepository.Setup(x => x.Save(It.IsAny<User>())).Verifiable();
    // Configure this repo in our Dependency Injection.
    ObjectFactory.Inject(typeof (IRepository<User>), mockUserRepository.Object);

    // Act.
    using (new TransactionScope())
    {
        UserService.Save(user);
        UnitOfWork.Commit(); // <-- not sure about this.. currently it's still
                             //     an EntityFramework context. 
                             //     I need to change this to.. something??

        // Assert.
        Assert.IsNotNull(user);
        Assert.IsTrue(user.UserId > 0);
    }
}

and the user service looks like this..

public class UserService : IUserService
{
    public UserService(IRepository<User> userRepository, 
                       ILoggingService loggingService)
    {
        // .. snip ..

        public void Save(User user) { .. }
    }
}

Any suggestions?


If you are unit testing UserService.Save(), then all your test should care about is that the repository is called. It's the responsibility of the repository tests to verify that an object is saved correctly.


That's actually something easy to do. You're setting up the Mock object like so:

mockUserRepository.Setup(x => x.Save(user)).Callback(() => user.UserId = 10); 
// mocking the value of 10 being insterted into the key

You can even continue using It.IsAny() if you want in the setup, but basically all you need to do is attach the callback to the end of your setup method.


> 1) How should I test a [UserService.]Save method? 
     Should I care about the result? and 
> 2) If i do care about #1 .. then how do i mock the 
     result also, so I can test that

If the repository is responsible to do the Identity-Magic then it makes no sense to mock this functionality in a test and then verify that this functionality has happened in the test. You want to test the UserService and not the IRepository-mock. The mock is there to crate a fake repository with just enought intelligence that the service does not crash and gets all requirements for the testcase. In this case I do not think that Identity-Magic is required by the service.

If the service is responsible to do the Identity-Magic then it makes sence to test if the id has been set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜