Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

swift - 重用SwiftyMocky生成的模拟(Re-using mocks generated by SwiftyMocky)

(This is a copy paste from my question posted on GitHub/SwiftyMocky)

((这是我在GitHub / SwiftyMocky上发布的问题的副本粘贴))

Introduction First of all, I'm relatively new to SwiftyMocky and my main experience with mocking is using Moq in a C# environment.

(简介首先,我是SwiftyMocky的新手,我的主要模拟经验是在C#环境中使用Moq。)

Although I am very impressed by SwiftyMocky, I am looking for an edge case that I cannot, or don't know how to, resolve with SwiftyMocky.

(尽管我对SwiftyMocky印象深刻,但是我正在寻找一个我无法或不知道如何使用SwiftyMocky解决的优势案例。)

Generating mocks from (external) libraries I'm working on a few libraries/frameworks that are dependent on each other and thus I require to generate mocks.

(从(外部)库生成模拟我正在研究彼此依赖的一些库/框架,因此我需要生成模拟。)

Sometimes I require mocked classes that are defined in other projects/libraries/frameworks.

(有时我需要在其他项目/库/框架中定义的模拟类。)

This is where my problem lies, I don't know how to properly manage this.

(这就是我的问题所在,我不知道如何正确管理它。)

1) It looks like SwiftyMocky only generates mock classes for files annotated in the current project.

(1)看起来SwiftyMocky只为当前项目中注释的文件生成模拟类。)

If I need to mock classes from other projects, I cannot annotate them.

(如果需要模拟其他项目中的类,则无法注释它们。)

Extending those classes and annotating them does not help because it appears SwiftyMocky then only mocks the extension code.

(扩展这些类并为它们添加注释无济于事,因为它看上去SwiftyMocky然后仅模拟扩展代码。)

The only solution I see is (re)creating a protocol definition for the class to be mocked, annotate it and use it in my test code.

(我看到的唯一解决方案是为要模拟的类创建(重新)协议定义,对其进行批注并在我的测试代码中使用它。)

- It's actually not a bad practise because as it requires to explicitly extend the actual class to conform to my protocol, when this external class has changed the compiler detects protocol non-conformity.

(-这实际上不是一个坏习惯,因为它需要显式扩展实际的类以符合我的协议,所以当此外部类已更改时,编译器会检测到协议不合格。)

This is - I guess - the way you should mock Apple or 3rd party classes?

(我猜这是模拟Apple或3rd Party类的方式吗?)

- When I am in control of the classes myself, this is a bit boilerplate, as I already created the classes in my source library.

(-当我自己控制类时,这有点样板,因为我已经在源库中创建了类。)

2) When creating classes in my source library, I normally provide them with a Protocol as well so my classes are dependent to the protocol instead of concrete implementation (SOLID, anyone?).

(2)在源库中创建类时,通常也向它们提供协议,因此我的类依赖于协议,而不是具体的实现(SOLID,有人吗?)。)

This means I -could- generate the mocks here is well.

(这意味着我可以在这里生成模拟了。)

I came up with the idea to generate two libs/frameworks, one with my concrete classes, and one with the generated mocks.

(我想到了生成两个库/框架的想法,一个是我的具体类,一个是生成的模拟。)

This way, the mocked code is in a separate library, and my unit test class header looks like:

(这样,模拟的代码位于单独的库中,而我的单元测试类标头看起来像:)

import XCTest
import TestLib
import TestLibMocks

class TestLibTests: XCTestCase {

The only problem here is that the generated classes by SwiftyMocky are open classes, and all protocol functions are public, except for the initializer, which are internal.

(这里唯一的问题是,SwiftyMocky生成的类是开放类,并且所有协议函数都是公共的,除了初始化程序是内部的。)

The test code below does not compile because the initialiser is internal:

(由于初始化程序是内部的,因此下面的测试代码无法编译:)

    func testMock() { 
        let mock = TestLibProtocolMock()  
    }

I resolved this by a post build/mock script where I replace the init(...) with public init(...) :

(我解决了这个由后生成/模拟脚本,我更换init(...)public init(...))

# Type a script or drag a script file from your workspace to insert its path.
swiftymocky generate
sed -i '' "s/    init/    public init/g"

TL;DR I don't know which mocking strategy in Swift is the way to go, so I am looking for some anchors here: - What would be the recommended way to re-use mock within Swift?

(TL; DR我不知道要在Swift中采用哪种模拟策略,因此我在这里寻找一些锚点:-在Swift中重用模拟的推荐方法是什么?)

Should I generate and supply mocks with my library, or should the code that requires the mocks create the protocol code for mocking, even when this leads to a bit boilerplate?

(我应该在我的库中生成并提供模拟,还是需要模拟的代码创建用于模拟的协议代码,即使这会导致一些样板工作?)

- Is this idea of generating and providing mock classes with your lib/framework a good idea anyway, and would it be a good feature anyway to generate public init for mocks?

(-无论如何,用lib / framework生成并提供模拟类的想法还是一个好主意,并且为模拟生成公共init还是一个好功能吗?)

(in that case, this issue can be considered a change request)

((在这种情况下,可以将此问题视为更改请求))

  ask by Tailz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...