【问题标题】:FakeItEasy: mocked method is not returning expected resultFakeItEasy:模拟方法未返回预期结果
【发布时间】:2016-05-31 22:06:55
【问题描述】:

我在单元测试中使用 FakeItEasy 作为模拟框架。方法 fakeUserService.AddUser 被模拟为返回新的 MwbeUser 对象,其中 AddUser 方法中有一些非空值

 A.CallTo(() => fakeUserService.AddUser(new MwbeUserRegistrationIn() 
                                            { 
                                                UserName = userName,                                                               
                                                FirstName = firstName, 
                                                SecondName = secondName, 
                                                Password = passwd, 
                                                Email = email, 
                                                BirthDate = birthdate 
                                            })).Returns(new MwbeUser 
                                                            { 
                                                                UserName = userName, 
                                                                Email = email, 
                                                                FirstName = firstName, 
                                                                SecondName = secondName, 
                                                                BirthDate = birthdate 
                                                            });

但它返回空值。为什么?

public void AddUserWithProperdata_UserIsAddedAndEmailIsGenerated()
        {
            // Arrange
            String userName = "user";
            String firstName = "Ala";
            String secondName = "ADsadas";
            String passwd = "passwd";
            String email = "kot@wp.pl";
            DateTime birthdate = DateTime.Today;

            fakeUserService = A.Fake<IMwbeUserService>();
            fakeAuthenticationService = A.Fake<IAuthenticationService>();

            A.CallTo(() => fakeUserService
                .AddUser(new MwbeUserRegistrationIn() { UserName = userName, FirstName = firstName, SecondName = secondName, Password = passwd, Email = email, BirthDate = birthdate }))
              .Returns(new MwbeUser { UserName = userName, Email = email, FirstName = firstName, SecondName = secondName, BirthDate = birthdate });

            MwbeUsersController controller = new MwbeUsersController(fakeUserService, fakeAuthenticationService);

            MwbeUserRegistrationJson userjson = new MwbeUserRegistrationJson
            {
                username = userName,
                passwd = passwd,
                firstname = firstName,
                secondname = secondName,
                email = email,
                birthdate = birthdate
            };

            // Act
            IHttpActionResult untypedResult = controller.AddUser(userjson);
            var test1 = untypedResult as OkResult;
            var test2 = untypedResult as CreatedNegotiatedContentResult<MwbeUser>;

            // Assert
            Assert.IsNotNull(untypedResult);
        }



  public interface IMwbeUserService
        {
            MwbeUser AddUser(MwbeUserRegistrationIn regData);
...
}

更新 1:添加控制器代码

[RoutePrefix("users")]
    public class MwbeUsersController : ApiController
    {
        IMwbeUserService userSrv;
        IAuthenticationService authService;


        public MwbeUsersController(IMwbeUserService service, IAuthenticationService authSrv)
        {
            this.userSrv = service;
            this.authService = authSrv;
        }

...

     [Route("register")]
            [HttpPost]
            public IHttpActionResult AddUser(MwbeUserRegistrationJson userdatadto)
            {
                // Validation
                if (null == userdatadto)
                {
                    return BadRequest("No user data");
                }

                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var registrationData = Conversion.ToUser(userdatadto);
                if(registrationData.Code != MwbeResponseCodes.OK)
                {
                    return BadRequest(registrationData.ErrorMessage);
                }

                // Registration
                try
                {
                    MwbeUser createdUser = userSrv.AddUser(registrationData.Data);
                    return Created<MwbeUser>("/users/" + createdUser.Id, createdUser); 
                }
                catch (UserAlreadyExistsException)
                {
                    return BadRequest("User with this username already exists");
                }
                catch (InvalidEmailAddress)
                {
                    return BadRequest("Given e-mail address is invalid");
                }

            }

【问题讨论】:

    标签: c# unit-testing mocking fakeiteasy


    【解决方案1】:

    我对 FakeItEasy 的经验很少,但我猜你的期望太严格了。

    只有在使用被认为相等的对象调用它时,对 AddUser 的期望才会匹配(就像使用 object.Equals(object, object) 一样。

    由于您的控制器传入了不同的 MwbeUserRegistrationIn 对象(registrationData):

    .AddUser(new MwbeUserRegistrationIn() {
        UserName = userName,
        FirstName = firstName,
        SecondName = secondName,
        Password = passwd,
        Email = email,
        BirthDate = birthdate }))
    

    而 MwbeUserRegistrationIn 大概不会覆盖object.Equals(object),这个期望不会被触发。您可以使用基于值的语义来实现MwbeUserRegistrationIn.Equals(object),也可以通过称为argument constraints 的东西放宽您的期望。

    在你的情况下,你可以稍微重写一下期望:

    A.CallTo(() => fakeUserService.AddUser(A<MwbeUserRegistrationIn>.That.Matches(u =>
        u.UserName == userName &&
        u.FirstName == firstName &&
        u.SecondName == secondName &&
        u.Password == passwd &&
        u.Email == email &&
        u.BirthDate == birthdate)))
        .Returns(new MwbeUser
        {
            UserName = userName,
            Email = email,
            FirstName = firstName,
            SecondName = secondName,
            BirthDate = birthdate
        });
    

    【讨论】:

    • @P.K.,@prgmtc 的代码中有一个小错字(不应该存在的})。我已经修改了答案中的示例。对我来说,它跑了又过去了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2010-09-22
    相关资源
    最近更新 更多