Rhino Mocks Sample How to Mock Property
Posted
by guazz
on Stack Overflow
See other posts from Stack Overflow
or by guazz
Published on 2010-04-10T11:37:15Z
Indexed on
2010/04/10
11:43 UTC
Read the original article
Hit count: 272
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")));
}
© Stack Overflow or respective owner