I am writing a unit test for a plugin using Dynamics CRM with RhinoMocks. After stubbing out the OrganizationService.Retrieve() method, when I invoke the stubbed out method, I am getting null back.
From what I can see (correct me if I'm wrong), is that the stubbed out method signature must the same as the invocation signature.
Here is my code:
TestSetup
var someGuid = Guid.Empty;
var organisationServiceMock = MockRepository.GenerateMock<IOrganizationService>();
organisationServiceMock.Expect(x => x.Retrieve("someCrmEntity", someGuid, SomeCrmEntityColumnSetQuery.ColumnSet)) .Return(new Entity { LogicalName = "someCrmEntity", Id = Guid.NewGuid(), });
SomeCrmEntityColumnSetQuery Code
public static class SomeCrmEntityColumnSetQuery
{
public static ColumnSet ColumnSet => new ColumnSet("column1", "column2");
}
Invocation Code
var someEntity = organisationServiceMock.Retrieve("someCrmEntity", someGuid, SomeCrmEntityColumnSetQuery.ColumnSet);
//someEntity is null
Things I have tried
- Removed the ColumnSet and replaced it with null - this works
- Replaced the static class SomeCrmEntityColumnSetQuery with a default instance (new ColumnSet())
- I have set the someGuid to Guid.Empty thinking that it was not "joining" on the correct Guid hence the null return value.
- I have tried to replace .Expect() with .Stub() - no joy
Edit In the expectation, I have tried the .WhenCalled(...) and that is how I found out that if I replace the columnSet argument with a null in the expectation and the invocation, it works. So it's go to do with something in my static class that represents a ColumnSet. The code works as I have it running in my DEV environment.
If anyone can share some light on this, that would be magic!
Charles
ColumnSet
should be public and the assignment should be=
instead=>
– SxntkOrganisationServiceMock
is a global variable? – SxntkIOrganizationService service = MockRepository.GenerateMock<IOrganizationService>();
– Sxntk