2015-08-18 55 views
1

在我的单元测试中使用FakeItEasy作为嘲讽框架。方法fakeUserService.AddUser被嘲笑到返回新MwbeUser对象与一些非空值的方法ADDUSERFakeItEasy:嘲笑方法没有返回预期结果

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 = "[email protected]"; 
      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>("https://stackoverflow.com/users/" + createdUser.Id, createdUser); 
       } 
       catch (UserAlreadyExistsException) 
       { 
        return BadRequest("User with this username already exists"); 
       } 
       catch (InvalidEmailAddress) 
       { 
        return BadRequest("Given e-mail address is invalid"); 
       } 

      } 

回答

4

的添加的代码我有FakeItEasy经验不多,但我的猜测是,你的预期是过于严格。

由于控制器在不同的MwbeUserRegistrationIn对象(registrationData)穿过如果它被称为与被认为是相等的(如,如果通过使用object.Equals(object, object)在物体上ADDUSER人们期望将只匹配

.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 
    }); 
+0

好点...我试着去实现它:)。 –

+0

你能写出完整的命令吗? A.CallTo(().....? –

+0

它给出了34个错误:) –