Rhino Mocks Assert Property Setter invoked with the correct object type

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 to verify that credentials were called using a valid NetworkCredential?

I tried this TestMethod but it fails because 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 verify that the setter was called with an object of type NetworkCredential and with the correct parameters?

+6
rhino-mocks
source share
2 answers

I fixed this by making ReportService an accepted network credential instead of 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); } 
+15
source share

Edit: Rethinking this issue, my previous suggested answer probably won't work. That's why:

Essentially, you are trying to verify that dependency dependency is configured correctly. This is probably why you are having trouble writing a unit test for this. You probably want to consider whether it makes sense to move the SetNetworkCredential method to a class that implements IReportExecutionService .

And if you do, the unit test for this method will be quite simple:

 [Test] public void TestSetNetworkCredential() { const string userName = "userName"; const string password = "password"; const string domain = "domain"; var rptService= new ReportExecutionService(); rptService.SetNetworkCredential(userName, password, domain); Assert.AreEqual(userName, rptService.Credentials.UserName); Assert.AreEqual(password, rptService.Credentials.Password); Assert.AreEqual(domain, rptService.Credentials.Domain); } 
+3
source share

All Articles