测试Swift扩展

时间:2017-05-30 09:16:27

标签: swift xctest quick-nimble nimble

我尝试创建一个cocoapod来扩展Swift中的Primitives。我有麻烦让测试通过或配置错误:

这是我的Nimble / Quick Test:

// https://github.com/Quick/Quick

import Quick
import Nimble
import SwiftRubySyntax


class TableOfContentsSpec: QuickSpec {
    override func spec() {
        describe("alphanumeric") {

            beforeEach {
                var validString = "abc"
                var invalidString = "abc12"
            }

            it("validates alphas to be true") {
                expect(validString).to(equal(validString)) // ***
            }

        }
    }
}

***我收到unresolved identifier "validString"错误

我真正想测试的是一个扩展。但变量也没有附加到我的字符串中:

public extension String {

    public var isAlpha: Bool {

        let alphaSet = CharacterSet.uppercaseLetters.union(.lowercaseLetters).union(.whitespacesAndNewlines)
        return self.rangeOfCharacter(from: alphaSet.inverted) == nil
    }

}

1 个答案:

答案 0 :(得分:2)

将变量置于beforeEach之外,然后将其设置在beforeEach

describe("alphanumeric") {
    var validString: String!
    var invalidString: String!

    beforeEach {
        validString = "abc"
        invalidString = "abc12"
    }
    ...
}