0
votes

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

  1. Removed the ColumnSet and replaced it with null - this works
  2. Replaced the static class SomeCrmEntityColumnSetQuery with a default instance (new ColumnSet())
  3. I have set the someGuid to Guid.Empty thinking that it was not "joining" on the correct Guid hence the null return value.
  4. 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

1
Please add the full test and the full code invoke, I want to know if your guids are the same. I think ColumnSet should be public and the assignment should be = instead =>Sxntk
Sxntk: Edited the codeh4ck3r8ug5
OrganisationServiceMock is a global variable?Sxntk
Nope. It's local. I forgot to copy the var.h4ck3r8ug5
You should declare the mock, before set the expectations IOrganizationService service = MockRepository.GenerateMock<IOrganizationService>();Sxntk

1 Answers

0
votes

So I found the answer after watching a PluralSight video on RhinoMocks.

My problem was that when setting up the stub, the stub does not take values but rather the signature of the method that you are stubbing out. For e.g:

var organisationServiceMock = MockRepository.GenerateMock();

//Wrong organisationServiceMock.Expect(x => x.Retrieve("someCrmEntity", someGuid, SomeCrmEntityColumnSetQuery.ColumnSet)).Return(new Entity());

//The stub does not care about what values are being sent into the method when invoked but rather if the method signature types match.

//Correct organisationServiceMock.Expect(x => x.Retrieve(Arg.Is.Anything, Arg.Is.Anything, Arg.Is.Anything)).Return(new Entity());

//During the invocation, stubbed method now expects the first argument to be a string, then 2nd to be a Guid, 3rd to be a ColumnSet.

I hope this helps anyone who has also been struggling with this. :)