我正在使用Rhino Mocks作爲單元測試的嘲諷框架。無法使用Rhino Mocks創建內部類型的模擬類
我有一個名爲Subject的類,它是我想測試的類。它依賴於IStore。
IStore定義如下:
//internal interface : has InternalsVisible to both "Subject"
//and "StoreTests" class namespaces
internal interface IStore {
void Store(string name);
//other methods
}
和主題類被定義爲如下:
class Subject : IStore {
private IStore internalStore;
//constructor injection
void Subject(IStore store) {
internalStore = store;
}
void Store(string name) {
internalStore.Store(name);
}
//other methods
}
使用RhinoMocks我的測試類如下:
//test class
class StoreTests {
Subject subject = new Subject();
[Test]
public StoreTest() {
//Arrange
var mockStore = MockRepository.GenerateMock<IStore>();
string testName = "test";
mockStore.Expect(x => x.Store(testName)).Returns(null);
//Act
subject.Store(testName);
//Assert
mockStore.VerifyAllExpectations();
}
//other test methods
}
在我的設置,界面被定義爲內部,並且它具有爲Subject類和Sto設置的InternalsVisible重新測試課程。但是,當測試用例執行時,它會在var mockStore = MockRepository.GenerateMock();時引發異常。 表示IStore不公開,因此無法生成模擬。
我認爲這是因爲IStore不公開。但是,由於我已經在IStore dll上設置了InternalsVisibleTo,它會不足以讓StoreTests爲該類創建一個模擬?
現在我認爲這個問題可以通過公開IStore接口來解決。但是鑑於這不是我的選擇,有沒有其他方法可以爲IStore創建模擬?
感謝您的鏈接!它現在有效。 – Santhosh