-
-
Notifications
You must be signed in to change notification settings - Fork 906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'afterEach' cannot be used inside 'it', 'afterEach' may only be used inside 'context' or 'describe' #1253
Comments
Also, it seems there is no way to test for memory leaks. The following example shows a memory leak and a test against it that is passing (it should fail). What is the correct approach for testing against memory leaks? class MyService {
var completion: (() -> Void)?
func perform(completion: @escaping () -> Void) {
self.completion = completion
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: completion)
}
}
class MyObject {
var didPerform = false
let service = MyService()
func perform(completion: @escaping () -> Void) {
service.perform {
self.didPerform = true
completion()
}
}
}
final class MockTest: QuickSpec {
override func spec() {
describe("mock test") {
var sut: MyObject!
beforeEach {
sut = MyObject()
}
afterEach { [weak sut] in
expect(sut).to(beNil())
}
it("performs") {
sut.perform {}
expect(sut.didPerform).toEventually(beTrue())
}
}
}
} |
context and describe are the same code implementation, just different meanings in the bdd world. Typically describe is used to describe your subject under test, while the context is used to describe your scenario for either interaction or state testing. The it block is only meant to contain a single test. For example
same with |
What did you do?
I am writing a test helper to help me find memory leaks.
When I initialise my SUT I have a method like so:
The problem is that in our company we have the convention of creating the SUT inside the
it
block. So when doing so I get the error'afterEach' cannot be used inside 'it', 'afterEach' may only be used inside 'context' or 'describe'
.I tried to replace the
afterEach
withaddTeardownBlock
but in that case the teardown block is never called. Is there any way to add support forafterEach
to be run inside theit
block, or add support foraddTeardownBlock
to be called after eachit
statement finishes?Further, if I move my
makeSUT
call inside adescribe
orcontext
scope, the SUT is never deallocated because it's held in memory by thatdescribe
orcontext
scope, so I can't test properly that it's being deallocated.So I assume the solution to this is to add support for
addTeardownBlock
inside anit
statement.What did you expect to happen?
addTeardownBlock
should be called after theit
statement.What actually happened instead?
addTeardownBlock
was never called, regardless where of the scope it was declared in (it
,describe
orcontext
)Environment
List the software versions you're using:
Xcode Default
Please also mention which package manager you used and its version. Delete the
other package managers in this list:
The text was updated successfully, but these errors were encountered: