Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks Assert Property Setter was called with correct object type

Tags:

rhino-mocks

I have a method that sets a property

public void SetNetworkCredential(string userName, string password, string domain)
{
    _reportExecutionService.Credentials = new NetworkCredential(userName, password, domain);
}

how do I verify that Credentials was called with a valid NetworkCredential?

I tried this TestMethod but it fails because the NetworkCredential objects are different references

[TestMethod]
public void TestTest()
{
    const string userName = "userName";
    const string password = "password";
    const string domain = "domain";

    var mock = MockRepository.GenerateMock<IReportExecutionService>();
    var rptService= new ReportService(mock);

    rptService.SetNetworkCredential(userName, password, domain);

    mock.AssertWasCalled(x => x.Credentials = new System.Net.NetworkCredential(userName, password, domain));
}

Is there a way to validate that the setter was called with an object of type NetworkCredential and with the correct parameters?

like image 972
Jon Erickson Avatar asked Jul 30 '09 23:07

Jon Erickson


1 Answers

I fixed it by making the ReportService accept a Network Credential instead of the username, password, domain

public void SetNetworkCredential(NetworkCredential networkCredential)
{
    _reportExecutionService.Credentials = networkCredential;
}

so my test was much easier

[TestMethod]
public void TestTest()
{
    const string userName = "userName";
    const string password = "password";
    const string domain = "domain";

    var mock = MockRepository.GenerateMock<IReportExecutionService>();
    var rptService= new ReportService(mock);

    var networkCredential = new System.Net.NetworkCredential(userName, password, domain);

    rptService.SetNetworkCredential(networkCredential);

    mock.AssertWasCalled(x => x.Credentials = networkCredential);
}
like image 151
Jon Erickson Avatar answered Sep 19 '22 07:09

Jon Erickson