Error When Mocking Crm IPluginExecutionContext

7 posts, 0 answers
  1. Brendon Birdoes
    Brendon Birdoes avatar
    4 posts
    Member since:
    Apr 2010

    Posted 12 Apr 2010 Link to this post

    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);

     

     

    }

  2. Code Monkey
    Code Monkey avatar
    1 posts
    Member since:
    Jan 2009

    Posted 13 Apr 2010 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 15 Apr 2010 Link to this post

    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.
  5. Brendon Birdoes
    Brendon Birdoes avatar
    4 posts
    Member since:
    Apr 2010

    Posted 15 Apr 2010 Link to this post

    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.
  6. Brendon Birdoes
    Brendon Birdoes avatar
    4 posts
    Member since:
    Apr 2010

    Posted 15 Apr 2010 Link to this post

    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.
  7. Brendon Birdoes
    Brendon Birdoes avatar
    4 posts
    Member since:
    Apr 2010

    Posted 16 Apr 2010 Link to this post

    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.
  8. Chris
    Admin
    Chris avatar
    221 posts

    Posted 19 Apr 2010 Link to this post

    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.
Back to Top
DevCraft R3 2016 release webinar banner