Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Unable to mock IClientChannel

2008-08-20
2013-04-29
  • Thomas Hetzer
    Thomas Hetzer
    2008-08-20

    Hi,

    I have a problem mocking the IClientChannel from System.ServiceModel. The code looks like this:

    > Mockery myMockery = new Mockery();
    > System.ServiceModel.IClientChannel c = myMockery.NewMock<System.ServiceModel.IClientChannel>();

    Every time I try to mock "System.ServiceModel.IClientChannel" I get a "TypeLoadException".
    Callstack:
    System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32 handle, Module module)
    System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
    System.Reflection.Emit.TypeBuilder.CreateType()
    NMock2.Monitoring.MockObjectFactory.CreateType(String typeName, Type mockedType)
    NMock2.Monitoring.MockObjectFactory.GetMockedType(TypeId id1, Type mockedType)
    NMock2.Monitoring.MockObjectFactory.CreateMockObject(Mockery mockery, Type mockedType, String name)
    NMock2.Mockery.NewMock(Type mockedType, String name)
    NMock2.Mockery.NewMock(Type mockedType)
    NMock2.Mockery.NewMock[InterfaceOfMock]()

    Any ideas what is wrong here? Anybody able to mock IClientChannel?

    Best regards
    Thomas

     
    • Urs Enzler
      Urs Enzler
      2008-08-21

      Hi Thomas

      I tried the following unit test with the current revision from the svn repository:

      using System.ServiceModel;
      using NUnit.Framework;

      namespace NMock2.BugReproduction
      {
          [TestFixture]
          public class MockServiceModelIClientChannelTest
          {
              [Test]
              public void MockServiceModelIClientChannel()
              {
                  Mockery mockery = new Mockery();
                  IClientChannel c = mockery.NewMock<IClientChannel>();

                  Expect.On(c).Method("Open");
                  c.Open();

                  mockery.VerifyAllExpectationsHaveBeenMet();
              }
             
          }
      }

      This works fine.
      Can you please tell me which revision or release you used?

      The above unit test is added to the NMock2 solution. Please get the current version from svn and try it for yourself (maybe I missed something - it's still early in the morning). You find the test in AcceptanceTeste.BugReproduction.MockServiceModelIClientChannel.

      If you can run the unit test within the NMock2 solution but not in your code then I suggest that you send me (ursenzler (at) users.sourceforge.net) the part of your project that fails.

      Best regards
      Urs