tags:

views:

194

answers:

2

How can I test that "TestProperty" was set a value when ForgotMyPassword(...) was called?

>    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")));

      }
+3  A: 
Assert.AreEqual(notificationSender.TestProperty, 1);
Alun Harford
Sorry Alun, I didn't refresh before I posted the same answer. +1
Bermo
> Is there a method to test that the property was set when the method was called? What if the property was set multiple times? e.g.: public void ForgotMyPassword(int userId) { User user = repository.GetUserById(userId); sender.Send("Changed password for " + user.Name); sender.TestProperty = 1; Reset(); sender.TestProperty = 2; Reset(); sender.TestProperty = 3; }
guazz
Stubs get property behaviour automatically. I believe you could check that the property was set with a particular value on a mock, but the caller of method you are testing should probably only care about the value of TestProperty when the method returns - I'd see this as a code smell.
Alun Harford
A: 
    [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);
    }
Bermo