Quick: itBehavesLike block executes before beforeEach

Created on 25 May 2016  路  6Comments  路  Source: Quick/Quick

I'm attempting to use a sharedExample to reduce some repetition in my code. It requires a differently configured type for testing so I'm passing it in via the context:

describe("with successful authentication") {

    var actualResponse: ServerResponse<ServerAuthenticationResponseData>!
    var boxedValue: Box<ServerResponse<ServerAuthenticationResponseData>>!

    beforeEach {
      (_, actualResponse) = stubCallAndWaitForSuccess(Fixtures.AuthenticationSessionSuccess, request: testRequest)
      boxedValue = Box(actualResponse)
    }

    itBehavesLike("populates the response") {
      ["actualResponse": boxedValue]
    }
}

I'm using a simple Box value because my ServerResponse<T> type is a struct and the dictionary requires an AnyObject for the value.

The tests are crashing when trying to access boxedValue inside of the itBehavesLike box because it executes before the beforeEach. Is this expected behavior? I'm not entirely sure how to use a sharedExample if it requires some setup in a beforeEach.

Thanks!

question

Most helpful comment

@michaelmcguire I'm currently experiencing the same issue (beforeEach is executing prior to my itBehavesLike call)... what did you discover you were doing wrong?

_UPDATE_: Nevermind, I figured it out. This was a classic "rubber ducky" situation, wherein I should have asked a rubber ducky my question before wasting the time of another human being.

For those whom are experiencing the same problem... I had the following pseudo-test:

class SomeQuickConfiguration: QuickConfiguration {
  override class func configure(_ configuration: Configuration) {
    sharedExample("some silly test") { (sharedExampleContext: @escaping SharedExampleContext) in
      var someStruct = sharedExampleContext()["myStruct"] as! MyStruct

      it("will not even get to this test") {
        // doesn't matter, little timmy has already died.
      }
    }
  }
}

class MyStructSpec: QuickSpec {
  override func spec() {
    var myStruct: MyStruct!
    beforeEach {
      myStruct = MyStruct()
    }

    describe("My struct does some config thang") {
      itBehavesLike("some silly test")
    }
  }
}

The crash I was receiving was a result of the forced unwrapping in the shared config. That was failing because the beforeEach in the MyStructSpec wasn't executing prior to the itBehavesLike. My solution was to move the forced unwrap in the shared config into a beforeEach block like you would any other spec. So the following:

class SomeQuickConfiguration: QuickConfiguration {
  override class func configure(_ configuration: Configuration) {
    sharedExample("some silly test") { (sharedExampleContext: @escaping SharedExampleContext) in
      var someStruct: MyStruct!
      beforeEach {
        someStruct = sharedExampleContext()["myStruct"] as! MyStruct
      }      

      it("will now execute this test") {
        // little timmy is now Lazarus
      }
    }
  }
}

All 6 comments

And a couple months after opening this issue, I have figured out what I was doing wrong. My bad! 馃槃

@michaelmcguire I'm currently experiencing the same issue (beforeEach is executing prior to my itBehavesLike call)... what did you discover you were doing wrong?

_UPDATE_: Nevermind, I figured it out. This was a classic "rubber ducky" situation, wherein I should have asked a rubber ducky my question before wasting the time of another human being.

For those whom are experiencing the same problem... I had the following pseudo-test:

class SomeQuickConfiguration: QuickConfiguration {
  override class func configure(_ configuration: Configuration) {
    sharedExample("some silly test") { (sharedExampleContext: @escaping SharedExampleContext) in
      var someStruct = sharedExampleContext()["myStruct"] as! MyStruct

      it("will not even get to this test") {
        // doesn't matter, little timmy has already died.
      }
    }
  }
}

class MyStructSpec: QuickSpec {
  override func spec() {
    var myStruct: MyStruct!
    beforeEach {
      myStruct = MyStruct()
    }

    describe("My struct does some config thang") {
      itBehavesLike("some silly test")
    }
  }
}

The crash I was receiving was a result of the forced unwrapping in the shared config. That was failing because the beforeEach in the MyStructSpec wasn't executing prior to the itBehavesLike. My solution was to move the forced unwrap in the shared config into a beforeEach block like you would any other spec. So the following:

class SomeQuickConfiguration: QuickConfiguration {
  override class func configure(_ configuration: Configuration) {
    sharedExample("some silly test") { (sharedExampleContext: @escaping SharedExampleContext) in
      var someStruct: MyStruct!
      beforeEach {
        someStruct = sharedExampleContext()["myStruct"] as! MyStruct
      }      

      it("will now execute this test") {
        // little timmy is now Lazarus
      }
    }
  }
}

No worries, @ryanbaldwin! I don't mind being your rubber ducky, so feel free to create issues/comment if you ever have any questions. :)

LOL.

it("will not even get to this test") {
    // doesn't matter, little timmy has already died.
}

I have to say - I laughed at this @ryanbaldwin. 馃槵

Glad you got it sorted out. :relaxed:

d3e736bed9c147b06e4c8ca7851c9a81

I guess the only thing that isn't clear for me right now is the chaining of beforeEach. How does it resolve? Outer first? It'd be great - likely in the QuickConfiguration section - if there were details of how the test setups are resolved... or are here details already and I failed to read?

Loving Quick + Nimble, btw. Great work. It's refuelled my passion for tests. I love the QuickConfiguration and the ability to reuse tests for common/similar components. That's what initially won me over and it continues to please me. Also -rad documentation. Y'all are hitting home runs.

Was this page helpful?
0 / 5 - 0 ratings