简体   繁体   English

Rhino Mocks-传递模拟类作为参数时,构造函数失败

[英]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. 除了我的测试类之外的所有内容都是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 . ApiClass1的构造ApiClass1接受IApiInterface作为参数,但是测试使用IApiInterface0调用它。 Change either of these, and the test will pass. 更改其中任何一个,测试将通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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