简体   繁体   中英

Rhino Mocks - constructor fails when passing mock class as argument

EDIT: I am entirely restating this question because I previously didn't articulate it correctly.

Everything except my test class is part of an API. I am passing a mock interface into a mock class, and then passing that mock class into my test class. My test class extends a class, and the body of the constructor is empty. My code breaks at my constructor, meaning the failure is in the base class constructor, whose implementation is hidden. I apologize if any of this is unclear, please let me know and I'll rephrase any parts that aren't clear. Thank you in advance for your help.

Here is a code example:

public class TestClass : ApiClass0
{
    ApiClass1 apiClass1;
    public TestClass(ApiClass1 apiClass1) : base(apiClass1)
    { 
        this.apiClass1 = apiClass1;
    }
    public void MethodToTest() 
    {
        apiClass1.Method0();
    }
}

public class ApiClass0
{
    public ApiClass0(ApiClass1 apiClass1) { }
}

public class ApiClass1
{
    public ApiClass1(IApiInterface i) {}
    public Method0() { }
}

public interface IApiInterface0 {}

public class TestClassTest
{
    [TestInitialize()]
    public void TestInitialize()
    {
        IApiInterface0 mApiInterface0 = MockRepository.GenerateMock<IApiInterface0>();
        ApiClass1 mApiClass1 = MockRepository.GenerateMock<ApiClass1>(mApiInterface0);
        TestClass testClass = new TestClass(mApiClass1); //code breaks here
    }
}

The constructor for ApiClass1 takes in IApiInterface as an argument, but the test invokes it with IApiInterface0 . Change either of these, and the test will pass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM