Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8046667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:51:46+00:00 2026-06-05T05:51:46+00:00

I am trying out RhinoMocks (only just started, please be gentle!) and am confused

  • 0

I am trying out RhinoMocks (only just started, please be gentle!) and am confused as to why if I use this setup:

var mockRepos = new MockRepository();
var mockServiceProvider = mockRepos.DynamicMock<IServiceProvider>(null);
var mockContext = mockRepos.DynamicMock<IPluginExecutionContext>(null);

mockServiceProvider.Expect(x => x.GetService(typeof(IPluginExecutionContext))).Return(mockContext);

var someSampleClass = new SomeClassOrOther(mockServiceProvider);

At some point in SomeClassOrOther, the method GetService is called

var context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

and causes the exception:

 Unable to cast object of type 'Castle.Proxies.ObjectProxyd0bf4b879a6341bbba3478cf1189d621' to type 'Microsoft.Xrm.Sdk.IPluginExecutionContext'.

However if I use:

var mockServiceProvider = MockRepository.GenerateMock<IServiceProvider>(null);
var mockContext = MockRepository.GenerateMock<IPluginExecutionContext>(null);

mockServiceProvider.Expect(x => x.GetService(typeof(IPluginExecutionContext))).Return(mockContext);

i.e. the static MockRepository.GenerateMock and it executes with no errors.

Where am I going wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:51:48+00:00Added an answer on June 5, 2026 at 5:51 am

    I don’t know why you are getting this very exception. However, when using the “old” non-static syntax, you get mocks in “Record” state. Before using them, you need to set them in Replay mode:

    var mockRepos = new MockRepository();
    var mockServiceProvider = mockRepos.DynamicMock<IServiceProvider>();
    var mockContext = mockRepos.DynamicMock<IPluginExecutionContext>();
    
    mockRepos.ReplayAll();
    
    mockServiceProvider
        .Stub(x => x.GetService(typeof(IPluginExecutionContext)))
        .Return(mockContext);
    
    var someSampleClass = new SomeClassOrOther(mockServiceProvider);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying out Linq for NHibernate and first noticed this: var list1 = (from ut
I'm trying to use Rhinomocks 3.5 and the new lambda notation to mock some
Just trying out this IDE but it already looks better than NetBeans in terms
I'm trying to work out why some of my test cases (using RhinoMocks 3.6
Just trying out Hibernate (with Annotations) and I'm having problems with my mappings. I
I'm currently trying out Aptana Studio 3 for PHP development (I'm pretty new to
been trying out this for quite some time but I'm still unable to built
Just trying out some C on some of the project euler questions. My question
Just trying out a couple of Android samples. Struggling to find the method managedQuery
We were just trying out the Azure Storage Analytics Service, and something very unusual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.