views:

64

answers:

2
[Test]
public void TestUserProfileInsert()
{
    using (new TestBindingsWrapper("TestBindings", "", new TestModule()))
    {
    // Setup the mock the dataprovider
    MyMocks.MockDataProvider.Setup(x => x.InsertUserProfile(It.IsAny<IUserProfile>())).Returns(1);

    IUserProfile up = new UserProfile();

    IUserProfileManager manager = new UserProfileManager();
    int result = manager.Insert(up);
    Assert.AreEqual(1, result);
    MyMocks.MockDataProvider.Verify(x => x.InsertUserProfile(up), Times.Once());
    }
}

I am using ninject also. It seems since I am setting the return value, what really is being tested, is this a joke or I am missing something?

A: 

It is testing that manager.Insert(up) returns 1, which I guess is the expected return code for success (or for failure. Then I would assume that the last line is verifying that the mock object you provided had a single insert on it. This basically tests to make sure UserProfileManager is correctly passing and using data from the data providers you are supplying.

NOTE: To make this test better I would recommend making the mock return 7 and then verify that 7 is being returned. There is a lower chance that the UserProfileManager object is just returning 7 that there is it is returning 1 all the time.

tster
but will it be inserting to the database? the insert returns the ID of the newly inserted object. the ID is the PK column from the db.
In this example I'm almost certain that the 1 is the return from a mock object. If you need to know more you should go talk to someone you work with that knows how the Mocks work. We can't magically tell you how the testing framework at your company is set up.
tster
A: 

When writing a test, you first want it to exercise the code to fail in the way you expect (either expected functionality of new code or current behavior). Only after that do you craft your code or modify the test to get things to pass. So, if this is your main question concerning this test, I recommend you step back and ask yourself what are you really trying to test.

The code you posted smells more like an Integration Test than a Unit Test, which are you trying to do? Your comments suggest you're worried about inserts into the database, yet you're mocking some objects. If a unit test, I'm surprised to see you are initializing so many concrete objects manually (e.g. UserProfileManager). Where does your MockDataProvider actually enter the execution path?

Brett Veenstra