Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to configure a "generic" sharedExample? #692

@nandin-borjigin

Description

@nandin-borjigin

I want to implement a ValueObjectSharedExampleConfiguration: QuickConfiguration.

    class ValueObjectSharedExampleConf: QuickConfiguration {
        override class func configure(_ configuration: Configuration) {
            sharedExamples("Value Object") {
                (context: @escaping SharedExampleContext) in
                describe("same objects") {
                    it("should be equal") {
                        let obj1a = context()["1a"]
                        let obj1b = context()["1b"]
                        expect(obj1a == obj1b).to(beTrue())
                    }
                }
                describe("different objects") {
                    it("should not be equal") {
                        let obj1 = context()["1a"]
                        let obj2 = context()["2"]
                        expect(obj1 == obj2).to(beFalse())
                    }
                }
            }
        }
    }

And then I want to test any classes/structs that conforms to Equatable with this shared Example like this:

itBehavesLike("Value Object") { [ "obj1a": foo1a, "obj1b": foo1b, "obj2": foo2] }

But the problem is, SharedExampleContext is actually a closure returns [String: Any], so obj1a, obj1b, obj2 variables I get in sharedExample closure are all of type Any, which doesn't necessarily conform to Equatable. Thus the code obj1a == obj1b won't compile.

Actually if I check obj1a is Equatable it returns true. But I don't know how to cast it to a proper type that compiler will accept. obj1a as! Equatable won't compile because Equatable is a generic protocol.

I can't just write obj1a as! Foo because if there is another class Bar: Equatable I want my sharedExample also works for that.

I tried to make the Configuration class generic, it compiled but crashed on itBehavesLike because that configuration class is not configured actually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions