jeudi 23 juin 2016

How can I unit test a computed (get only) property in Swift?

I wrote the following class extension on NSURL:

extension NSURL
{
    var linkComponents: LinkComponents
    {
        var pathComponents = self.pathComponents ?? []

        if let first = pathComponents.first where first == "/"
        {
            pathComponents.removeFirst()
        }

        return LinkComponents(components: pathComponents)
    }

    var linkPath: String
    {
        return self.path ?? ""
    }
}

It contains a computed (get-only) property:

var linkComponents: LinkComponents

When I attempt to test this computed property in my unit test target, the compiler throws and error saying Ambiguous use of 'linkComponents' and suggests a few "candidates".

This seems like an access control issue. My import statement in my test file has the @testable annotation. All of my other tests (for non-computed properties) compile a-ok without me having to specify any access control beyond the default internal. And if I convert this particular computed property into a function, the test target compiles a-ok.

Is it possible to unit test computed properties in Swift? If so, how? It goes without saying that (1) I don't want to convert this computed property to a function and (2) I don't want to add public to everything.

Aucun commentaire:

Enregistrer un commentaire