在使用IoC时,MustHaveHappened没有被调用

时间:2012-11-12 20:48:01

标签: c# unit-testing mocking structuremap fakeiteasy

我正在尝试测试服务调用。 我有IAuthenticationService拨打UpdateUserProfile电话。 IAuthenticationService位于IoC中(我的情况为StructureMap)。 MustHaveHappened正在返回0次调用,但我知道这是在调试时发生的。

[TestMethod]
public void It_should_call_the_UpdateUserProfile()
{
    // initialize in the base class, here for brevity
    ObjectFactory.Initialize(registry =>
    {
        registry.For<IAuthenticationService>().Use(A.Fake<IAuthenticationService>());
    });
    UserProfile profileSend = new UserProfile
    {
        UserName = this.HttpContextBaseFake.User.Identity.Name,
        CultureSelection = expectedCultureInfoString
    };

    Utils.UpdateProfile(profileSend);
    IAuthenticationService service = ObjectFactory.GetInstance<IAuthenticationService>();
    UserProfile profileExpect = new UserProfile
    {
        UserName = this.HttpContextBaseFake.User.Identity.Name,
        CultureSelection = expectedCultureInfoString
    };

    // this is returning 0 calls
    // I've also tried sending in the profileExpect instead of A.Dummy<UserProfile>
    // it would be nice to test for the name and string was sent
    A.CallTo(() => service.UpdateUserProfile(A.Dummy<UserProfile>())).MustHaveHappened();
}

// using static class for a reason, the code in my project has more in it
public static class Utils
{
    public static void UpdateProfile(UserProfile profile)
    {
        // user profile is more easily available, use that for the unique identifier
        IAuthenticationService service = ObjectFactory.GetInstance<IAuthenticationService>();
        UserProfile userProfile = new UserProfile
        {
            UserName = userProfileName,
            CultureSelection = cultureInfoString
        };

        service.UpdateUserProfile(userProfile);
    }
}

1 个答案:

答案 0 :(得分:1)

我需要在A.CallTo中使用A.Ignored而不是A.Dummy()。(。

所以它变成了:

A.CallTo(() => service.UpdateUserProfile(A<UserProfile>.Ignored)).MustHaveHappened();
相关问题