Swift/Quick/Nimble with async/await does not compile

1.2k Views Asked by At

I am trying to write a unit test using Quick/Nimble that tests an async function, as per the Nimble documentation:

await expect(await aFunctionReturning1()).to(equal(1))

Unfortunately this line does not compile for me.

Environment:

  • Xcode 14.2
  • Quick 6.1.0
  • Nimble 11.2.1
  • Test in a Swift Package

A minimal reproducable example is this:

import Quick
import Nimble

struct Foo {
    func bar() async -> String {
        await Task { "Hello World" }.value
    }
}

final class FooSpec: QuickSpec {
    override func spec() {
        describe("A Foo") {
            describe("When calling Bar") {
                var foo: Foo!
                beforeEach {
                    foo = Foo()
                }
                it("Says Hello World") { await expect( await foo.bar()) == "Hello World" }
            }
        }
    }
}

This leads to 'async' call in an autoclosure that does not support concurrency, and another warning I do not understand: Compiler error

What am I missing?

2

There are 2 best solutions below

1
Lasse On

After having raised the issue on the Quick/Nimble github repo I got feedback that suggest the documentation is simply wrong.

Instead of using round brackets as the documentation suggests:

await expect( await foo.bar()) == "Hello World"

we should use curly brackets like this:

await expect { await foo.bar() } == "Hello World"

This line compiles and works as expected.

0
g0tcha- On

You need to inherit from 'AsyncSpec'.

final class FooSpec: AsyncSpec {...}

For more information: https://github.com/Quick/Quick/blob/main/Documentation/en-us/AsyncAwait.md