This is a migrated thread and some comments may be shown as answers.

Error When Mocking Crm IPluginExecutionContext

6 Answers 175 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brendon Birdoes
Top achievements
Rank 1
Brendon Birdoes asked on 13 Apr 2010, 01:03 AM
I am evaluating the framework for mocking the IPluginExecutionContext interface that is critical to mocking Microsoft Crm Plug-ins.  This is a good test of the framework because on the same interface, I need a dictionary property to essentially be stubbed and then some methods to be mocked.  Also the property can use indexers to get to the values and there is only a getter for the interface, so I cannot just set the property to a new dictionary (actually the class is a custom Crm class called PropertyBag, but is similar to a dictionary).

I receive the error:
Test method Ryland.Crm.Plugins.Tests.ContactRequiredFieldsPluginTest.ExecuteRequiredFieldTelerikTest threw exception System.AccessViolationException, but exception Microsoft.Crm.Sdk.InvalidPluginExecutionException was expected. Exception message:  System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Telerik.DynamicProxy.ProxyFactory.Create()
Telerik.DynamicProxy.Fluent.FluentProxy.NewInstance()
Telerik.DynamicProxy.Proxy.Create(Type target, Action`1 action)
ctor(Type target, BehaviorMode mode, Object[] args)
Telerik.JustMock.Mock.Create(Type target, BehaviorMode mode, Object[] args)
Telerik.JustMock.Mock.Create(Type target, Object[] args)
Telerik.JustMock.Mock.Create[T]()
Ryland.Crm.Plugins.Tests.ContactRequiredFieldsPluginTest.ExecuteRequiredFieldTelerikTest() in C:\projects\RylandStandards\RylandStandards\tests\Ryland.Crm.Plugins.Tests\ContactRequiredFieldsPluginTest.cs: line 58


My code is below.  Also note I am running on a Hyper-V Guest and do have references to other Mocking frameworks in the project since I am comparing and contrasting Rhino Mocks, Moq and JustMock.  I have JustMock enabled.  The Microsoft Crm sdk contains the IPluginExecutionContext and only the sdk is needed to test this scenario, not Crm.

If the syntax below works for this test, I will definitely recommend this framework.  The simplicity is great compared to RhinMocks and slightly better than Moq.  Understanding the API is important to adoption and the ability to avoid needing different calls for stubbing or mocking is very nice.  I also am assuming the typical Create will essentially cause properties to behave like simple get/set properties without any setup.

///

 

<summary>

 

 

///

 

 

///</summary>

 

[

TestMethod()]

 

[

ExpectedException(typeof(InvalidPluginExecutionException))]

 

 

public void ExecuteRequiredFieldTelerikTest()

 

{

 

 

 

ContactRequiredFieldsPlugin target = new ContactRequiredFieldsPlugin(string.Empty, string.Empty);

 

 

var context = TelerikMock.Mock.Create<IPluginExecutionContext>(); //!!!!!!!!! Fails here

 

 

var service = TelerikMock.Mock.Create<ICrmService>();

 

 

TelerikMock.Mock.Arrange(() => context.CreateCrmService(true)).Returns(service);

 

 

DynamicEntity

 

entity = new DynamicEntity(EntityName.contact.ToString());

 

entity.Properties.Add(

new StringProperty("telephone1", ""));

 

entity.Properties.Add(

new StringProperty("emailaddress1", ""));

 

 

 

context.InputParameters[

ParameterName.Target] = entity;

 

target.Execute(context);

 

 

}

6 Answers, 1 is accepted

Sort by
0
Code Monkey
Top achievements
Rank 1
answered on 13 Apr 2010, 07:15 AM
Hi Brendon,
Thats nice that you liked the framework. I will try this out today and let you know what's the issue.

Regards,
Mehfuz
0
Ricky
Telerik team
answered on 15 Apr 2010, 01:43 PM
Hi Brendon,

Thanks for your nice comments. Now, to reproduce the scenario , i cratead a MSTest class added Rhino, Moq and JustMock (From public site) references and added the following test.

So, far it looks like things are working here :

[TestMethod]

[ExpectedException(typeof(InvalidPluginExecutionException))]

public void ShouldAssertPluginContext(){

var context = Mock.Create<IPluginExecutionContext>();

var service = Mock.Create<ICrmService>();

Mock.Arrange(() => context.CreateCrmService(true)).Returns(service);

ICrmService crm = context.CreateCrmService(true);

Assert.AreEqual(service.GetHashCode(), crm.GetHashCode());

}

I have installed CRM 4.0 sdk then did the above, i also checked IPluginExecucutionContext interface, through reflection and looks like nothing fishy there. Also, just to be sure that we are  on the right page, can you turn off the profier from justmock menu and run the sample again and then turn it on  and then try again and let me know the result before and after.

Also, do you have Typemock installed. As , both JustMock and Tyemock registers a profiler for mocking non virutal stuffs, so having both can override each other. 

Again, any help will be much apprecitated !!

Regards,
Mehfuz




Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Brendon Birdoes
Top achievements
Rank 1
answered on 15 Apr 2010, 04:28 PM
TypeMock is not installed.  I will try running your test below and also turn off the profiler and let you know if I see any difference.
0
Brendon Birdoes
Top achievements
Rank 1
answered on 15 Apr 2010, 04:40 PM
I resolved the issue.  I added the reference to the JustMock assembly and received the odd error mentioned in my first post. When running the test you responded with I was told I was missing the CodeWeaver.Hook assembly.   I added the assembly to the project references and it now runs correctly.

Did I miss some step in the installation of the product?  Will I always need to carry around two-three assemblies?  Seems like a multi-module assembly would be a good solution here.
0
Brendon Birdoes
Top achievements
Rank 1
answered on 16 Apr 2010, 05:18 PM
I was able to reproduce the issue on a separate machine even with the assemblies added.  The previous machine was a local virtual machine and this is a regular machine.  Both are running Visual Studio 3.5 SP1 and Windows Server 2008.
0
Chris
Telerik team
answered on 19 Apr 2010, 01:45 PM
Hi Brendon,
This looks quite strange. Generally you wouldn't need to add a reference to Telerik.CodeWeaver.Hook assembly as it's referenced internally by the JustMock assembly so if you're referencing Telerik.JustMock.dll, the testing framework should copy the CodeWeaver.Hook assembly to the output folder too. You should only take care that the folder from which you're referencing the JustMock assembly should contain the CodeWeaver.Hook assembly too.
Hope this helps.

Sincerely yours,
Chris
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
General Discussions
Asked by
Brendon Birdoes
Top achievements
Rank 1
Answers by
Code Monkey
Top achievements
Rank 1
Ricky
Telerik team
Brendon Birdoes
Top achievements
Rank 1
Chris
Telerik team
Share this question
or