我有一个框架Whiteboard
,它封装了我的业务逻辑。我试图保持我的依赖倒置和解耦,因为Whiteboard
取决于存储库,所以它声明了一个协议WhiteboardRepository
,并且它期望与Whiteboard
链接的客户端提供WhiteboardRepository
的实现。如何将XCTestCases公开到外部测试包?
你可以在下面的屏幕截图中看到这个。还请注意WhiteboardTests
组,其中包括WhiteboardRepositoryTests
类以及WhiteboardRepositoryFake
及其自己的测试子类。
为了确保预期WhiteboardRepository
的行为的实施方式中,WhiteboardTests
测试包定义了WhiteboardRepositoryTests
子类的XCTestCase
:
class WhiteboardRepositoryTests: XCTestCase {
var repo: WhiteboardRepository?
override func setUp() {
if let repo = repo {
// test setup here
}
}
// test cases here
}
为了的Whiteboard
一个客户机来测试其执行WhiteboardRepository
,实现的测试类将子类WhiteboardRepositoryTests
划分为子类,并将实现的实例提供给测试子类,然后使用该实例在运行测试时。
例如,这里是WhiteboardRepositoryFakeTests
样子:
class WhiteboardRepositoryFakeTests: WhiteboardRepositoryTests {
override func setUp() {
repo = WhiteboardRepositoryFake()
super.setUp()
}
// the test classes run, using the instance of WhiteboardRepositoryFake()
}
这工作得很好,当然,因为WhiteboardRepositoryFakeTests
是在WhiteboardTests
捆绑,所以WhiteboardRepositoryTests
暴露WhiteboardRepositoryFakeTests
。
的问题是:,对Whiteboard
链接的应用程序将需要创建自己的WhiteboardRepositoryTests
子类来测试自己的实现,而是因为他们没有进入WhiteboardTests
测试包,他们不知道WhiteboardRepositoryTests
的类,所以不能继承它。
我有多个客户端占用Whiteboard
,所以我不能WhiteboardRepositoryTests
类简单地复制到每个客户端和也不会我想,因为它的Whiteboard
责任界定WhiteboardRepository
的行为,所以WhiteboardRepositoryTests
应住在Whiteboard
的测试包中。在一个理想的世界中,我可以将Whiteboard
的测试包链接或注入到客户端的测试包中,以便WhiteboardRepositoryTests
公开给客户端的测试,但我不明白我该如何做到这一点。
有没有办法绕过这个障碍?如何将WhiteboardRepositoryTests
公开到客户端测试包中的测试,以便客户端可以确保其实现WhiteboardRepository
的行为与预期相符?
这听起来像个好主意,但是当我尝试它时,我得到了'无法为'XCTest''加载底层模块。当我尝试向桥接头添加'#import'时,“加载模块”错误消失,但是在桥接头中出现'XCTest/XCTest.h'文件未找到'错误,这是特别令人困惑的,因为'XCTest.h'正好在我拖入的框架中。 –
挂起,我想也许我明白了 - 至少是第一部分。我不得不从字面上将'XCTest'框架复制到我的项目中,并添加到框架搜索路径中以直接指向磁盘。然后,我必须将Whiteboard框架链接到“WhiteboardContractTests”,最后,我必须将测试文件目标成员资格赋予“WhiteboardTests”测试包。我现在就去尝试做客户端的工作。 –
嗯,我对XCTest并不十分清楚,但我认为你已经明白了。 –