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
itBehavesLike is not invoking beforeEach #678
Comments
Hey @Nandiin, Thanks for filing an issue 😄 . The behavior of |
@Nandiin @jeffh class FooSharedExampleConf: QuickConfiguration {
override class func configure(_ conf: Configuration) {
sharedExample("foo") {
var bar:Bar!
var baz:Baz!
setupExample() { context: SharedExampleContex in
let bar = context()["bar"] as! Bar // This line is causing crash
let baz = context()["baz"] as! Baz // This line would crash too
}
it("some test") {
expect(bar.baz) == baz
}
it("another test") {
expect(bar.baz2) == baz
}
}
}
} where the Quick framework would have to be responsible for running the This way one cannot run the Let me know your thoughts or if I'm missing something |
Hey @CaioSym, thanks for commenting. That's an interesting solution, although it does cause problems if one wants to use it inside another closure. An example is passing along constants through a context: class FooSharedExampleConf: QuickConfiguration {
override class func configure(_ conf: Configuration) {
sharedExample("foo") { context in
var bar: Bar!
var baz: Baz!
beforeEach {
bar = context()["bar"] as! Bar
baz = context()["baz"] as! Baz
}
it("some test") {
expect(bar.baz) == context()["expectedBaz"] as! Baz
}
it("another test") {
expect(bar.baz2) == context()["expectedBaz"] as! Baz
}
}
}
} Maybe it's not worth having that available to |
@jeffh I may be missing something crucial here but it seems to at worst case one could simply do as follows: class FooSharedExampleConf: FooSharedExampleConf {
override class func configure(_ conf: Configuration) {
sharedExample("foo") {
var context: SharedExampleContex!
var staticContextResult: [Hashable: Any]! // or specific type in case of strongly typed contexts
var bar:Bar!
var baz:Baz!
setupExample() { ctx: SharedExampleContex in
context = ctx
staticContextResult = ctx()
bar = ctx()["bar"] as! Bar // This line is causing crash
baz = ctx()["baz"] as! Baz // This line would crash too
}
beforeEach() {
//do Something with context
}
it("some test") {
expect(bar.baz) == context()["expectedBaz"] as! Baz
}
it("another test") {
expect(bar.baz) == staticContextResult["expectedBaz"] as! Baz
}
}
}
} The main point is making it explicit that context() is likely a dependent on a |
There is no wonder why did Nandiin expect that behavior of And it would be a good idea, to let it work this way. The shared example could be much more reusable and make more sense for unit tests. Currently, the solution is to create |
At the very least, the documentation should be updated, since as @Arcikbtw mentions the documentation implies that |
What did you do?
I'm using shared examples and find that
itBehavesLike
doesn't behave likeit
--itBehavesLike
won't triggerbeforeEach
itself, so any forced unwrapping variables shouldn't be accessed directly withinsharedExample
closure. One should move that unwrapping either intoit
closure orbeforeEach
closure inside thesharedExample
The code follows demonstrates the problem
beforeEach
closure inBarBazSpec
won't be called foritBehavesLike
untill it reaches ait
closure, so when the code runs into commented lines inFooSharedExampleConf
thebeforeEach
closure hasn't been executed thusbar
andbaz
is still nil.Making some change like this will correct the problem, but I consider it as unintuitive.
First of all,
itBehavesLike
has an "it" in it and that's why I thought it should triggerbeforeEach
likeit
closure does. And as it can be seen from the code above, adding anotherbeforeEach
closure inside thesharedExample
is verbose.I suppose
itBehavesLike
actually behaves more likedescribe
andcontext
because it is meant to have severalit
in it. From this perspective, this makes sense, but I'm still wondering if there is better solution to this.What did you expect to happen?
I thought
itBehavesLike
itself triggersbeforeEach
closure itselfWhat actually happened instead?
itBehavesLike
doesn't triggerbeforeEach
closureEnvironment
List the software versions you're using:
The text was updated successfully, but these errors were encountered: