2010-04-10 5 views
2

Comment puis-je tester que "TestProperty" a été défini comme valeur lorsque ForgotMyPassword (...) a été appelé?Rhino Mocks Exemple de simulation de propriété

> public interface IUserRepository  
    { 
     User GetUserById(int n); 

    } 
    public interface INotificationSender 
    { 
     void Send(string name); 
     int TestProperty { get; set; } 
    } 

    public class User 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 

    public class LoginController 
    { 
     private readonly IUserRepository repository; 
     private readonly INotificationSender sender; 

     public LoginController(IUserRepository repository, INotificationSender sender) 
     { 
      this.repository = repository; 
      this.sender = sender; 
     } 



     public void ForgotMyPassword(int userId) 
     { 
      User user = repository.GetUserById(userId); 
      sender.Send("Changed password for " + user.Name); 
      sender.TestProperty = 1; 
     } 
    } 

    // Sample test to verify that send was called 
    [Test] 
    public void WhenUserForgetPasswordWillSendNotification_WithConstraints() 
    { 
     var userRepository = MockRepository.GenerateStub<IUserRepository>(); 
     var notificationSender = MockRepository.GenerateStub<INotificationSender>(); 

     userRepository.Stub(x => x.GetUserById(5)).Return(new User { Id = 5, Name = "ayende" }); 

     new LoginController(userRepository, notificationSender).ForgotMyPassword(5); 

     notificationSender.AssertWasCalled(x => x.Send(null), 
      options => options.Constraints(Rhino.Mocks.Constraints.Text.StartsWith("Changed"))); 

     } 

Répondre

4
Assert.AreEqual(notificationSender.TestProperty, 1); 
+0

Désolé Alun, je ne l'ai pas rafraîchir avant posté la même réponse. +1 – Bermo

+0

> Existe-t-il une méthode pour tester que la propriété a été définie lors de l'appel de la méthode? Que faire si la propriété a été définie plusieurs fois? par exemple: public void ForgotMyPassword (int userId) { Utilisateur utilisateur = repository.GetUserById (ID utilisateur); sender.Send ("Mot de passe modifié pour" + user.Name); sender.TestProperty = 1; Réinitialiser(); sender.TestProperty = 2; Réinitialiser(); sender.TestProperty = 3; } – guazz

+1

Les talons obtiennent le comportement de propriété automatiquement. Je crois que vous pouvez vérifier que la propriété a été définie avec une valeur particulière sur un simulacre, mais l'appelant de la méthode que vous testez devrait probablement se préoccuper uniquement de la valeur de TestProperty lorsque la méthode retourne - je verrais ça comme une odeur de code. –

1
[TestMethod] 
    public void WhenUserForgetPassword_TestPropertyIsSet() 
    { 
     var userRepository = MockRepository.GenerateStub<IUserRepository>(); 
     var notificationSender = MockRepository.GenerateStub<INotificationSender>(); 

     userRepository.Stub(x => x.GetUserById(Arg<int>.Is.Anything)).Return(new User()); 

     notificationSender.TestProperty = 0; 
     new LoginController(userRepository, notificationSender).ForgotMyPassword(0); 

     Assert.AreEqual(notificationSender.TestProperty, 1); 
    } 
Questions connexes