views:

1992

answers:

1

I've have searched on this and it seems to be a catch all, unfortunately everything I've read doesn't help figure it out. Here is the class:

public interface IMockInterface
{
    MockClass MockedMethod();
    MockClass MockThis();
}

public class MockClass : IMockInterface
{
  public virtual MockClass MockedMethod()
  {
    MockClass returnValue;

    returnValue = new MockClass();
    returnValue.SomeMessage = "Not mocked";
    return returnValue;
  }

  public MockClass MockThis()
  {
    MockClass mock;
    MockClass returnValue;

    mock = new MockClass();

    return mock.MockedMethod();
  }
}

And the test:

public void MockTest_Internal()
{
  MockClass mainClass;
  MockClass returnedClass;
  IMockInterface mockProvider;

  mainClass = new MockClass();

  mockProvider = repository.StrictMock<IMockInterface>();
  Expect.Call(mockProvider.MockedMethod())
    .Return(new MockClass { SomeMessage = "Mocked" });
  repository.ReplayAll();

  returnedClass = mainClass.MockThis();
  provider.AssertWasCalled(item => item.MockedMethod());

  Assert.IsTrue(returnedClass.SomeMessage == "Mocked");
}

And have also tried and doesn't work

But I keep getting this exception:

Rhino.Mocks.Exceptions.ExpectationViolationException:
IMockInterface.MockedMethod(); Expected #1, Actual #0

Now from what I've read this would suggest either the method was called with different than expected parameters OR the method was never called but was expected to be called. This isn't the case for the test.

Side Note: This is my first time really using Rhino.Mocks without some in house code so I am basically picking it up as I go. There could be something really stupid here...

This was the old test commented on, but is not what I should have been using:

public void MockTest_Internal()
{
  MockClass mainClass;
  MockClass returnedClass;
  IMockInterface mockProvider;

  mainClass = new MockClass();

  var provider = MockRepository.GenerateStub<IMockInterface>();
  provider.Stub(item => item.MockedMethod())
    .Return(new MockClass { SomeMessage = "Mocked" });

  returnedClass = mainClass.MockThis();
  provider.AssertWasCalled(item => item.MockedMethod());

  Assert.IsTrue(returnedClass.SomeMessage == "Mocked");
}
+1  A: 

You're telling the mock framework to stub the MockedMethod class on the provider object, but you never inject the provider into the mainClass object to be used. It's not clear to me what you are trying to accomplish but if you want the mocked method to be called then it has to be called on the object on which the stub was set up.

If you define MockThis as below, I think you will find that it will work.

public MockClass MockThis(IMockInterface provider)
{
    return provider.MockMethod();
}

The bottom line is that you get the exception because the method was never called on the provider, only on the mainClass object.

EDIT: Example

public class ClassUnderTest
{
    private ProviderClass provider { get; set; }

    public ClassUnderTest( ProviderClass provider )
    {
        this.Provider = provider;
    }

    public int DoOperation()
    {
        return this.Provider.ProviderOperation();
    }
}

public class ProviderClass
{
    private int value = 42;
    public ProviderClass()
    {
    }

    public virtual int ProviderOperation()
    {
        return this.value;
    }
}


[TestMethod]
public void DoOperationTest()
{
     ProviderClass mockProvider = MockRepository.GenerateMock<ProviderClass>();
     mockProvider.Expect( mp => mp.ProviderOperation() ).Return( -1 );

     ClassUnderTest target = new ClassUnderTest( mockProvider );

     int expectedValue = -1;
     int value = target.DoOperation();

     Assert.AreEqual( expectedValue, value );

     mockProvider.VerifyAllExpectations();
}

Normally the ProviderClass object would return 42 from the ProviderOperation method, but we've mocked it out and told it to return -1. When the ClassUnderTest DoOperation method is called, the mock provider object's ProviderOperation method is invoked and returns the mocked value of -1.

Hope this helps.

tvanfosson
I'm just trying to create a junk class to learn how to mock. Basically I created the class to have a method that would be called within a method and return something.
Programmin Tool
The mocked object is a specific object, though. The code you are testing will need to use the mocked object directly in order for the stubbed/mocked methods to be invoked.
tvanfosson
In all honesty, I could use the second example if it would work. Right now I'm just an idiot child trying to put an airplane together with a box of parts.
Programmin Tool
Ok so first test example was bad as you noted. Stub wasn't what I needed. However, I did have the second example that still didn't work.
Programmin Tool
Answered one question, only brought on more.
Programmin Tool