swiftxctestquick-nimblenimble

Testing Swift Extensions


I try to create a cocoapod to extend Primitives in Swift. I have troubles to get tests passing or have an misconfiguration:

Here is my 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)) // ***
            }

        }
    }
}

*** I get an unresolved identifier "validString"error

What I really wanna Test is an extension. But the variables are not attached to my strings too:

public extension String {

    public var isAlpha: Bool {

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

}

Solution

  • Have your variables outside of the beforeEach and then set them in the beforeEach

    describe("alphanumeric") {
        var validString: String!
        var invalidString: String!
    
        beforeEach {
            validString = "abc"
            invalidString = "abc12"
        }
        ...
    }