-1

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?

Lasse
  • 490
  • 3
  • 8
  • You can watch Meet async/await. You have to convert the closures to use continuations, they can’t be mixed – lorem ipsum Mar 20 '23 at 13:20
  • There should be no need to convert closures. Nimble docs say `If you use Quick, then you don't need to do anything because as of Quick 6, all tests are executed in an async context.`. – Lasse Mar 20 '23 at 13:26
  • Also, looking at Nimble's source code there are several versions of `expect` that are both async itself and have an async closure, e.g. `public func expect(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> T)) async -> AsyncExpectation` – Lasse Mar 20 '23 at 13:29
  • is titleYou refers to a chapter of the documentation that says "Expectations Using expect(...).to" and yet you use == in your code. What happens if you do `await expect( await foo.bar()).to(equal("Hello World!"))` instead? – Joakim Danielson Mar 20 '23 at 13:55
  • @JoakimDanielson: Just tried it - same result :( – Lasse Mar 20 '23 at 14:09

2 Answers2

0

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

g0tcha-
  • 323
  • 3
  • 5
-1

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.

Lasse
  • 490
  • 3
  • 8
  • I would suggest that since this is a trivial mistake in the docs, the approprate and most helpful path is to fork, fix the docs, and make a pull request — not to make this a Stack Overflow entry. – matt Mar 27 '23 at 16:40