Skip to content
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

BeforeEach not executing - what am I missing? #445

Closed
Jtango18 opened this issue Dec 18, 2015 · 7 comments
Closed

BeforeEach not executing - what am I missing? #445

Jtango18 opened this issue Dec 18, 2015 · 7 comments
Labels

Comments

@Jtango18
Copy link

Hi all,

I'm pretty new to Quick, but I'm having an issue with the most basic of scenarios, hoping someone can point to something very simple I'm doing wrong.

I have the follow spec setup

class TripSpec : QuickSpec
{
    override func spec() {
        describe("a trip") {
            var trip: Trip!
            beforeEach {
                trip = Trip(tripName: "Test Trip", purpose: 0)
                print("Trip Created")
            }
            describe("its distance"){
                context("after adding a segment"){
                    let segment = TripSegment(coordinates: [CLLocationCoordinate2D()], distance: 37.5);
                    trip.addSegment(segment)
                    it("has added distance") {
                        expect(trip.distance).to(beCloseTo(37.5, within:0.1));
                    }
                }
            }
        }
    }
}

The problem I'm having is that the before each doesn't seem to be getting called. When I try to access my variable "trip" in the context and in the it statement, it is nil.

Any help would be much appreciated.

Cheers

JT

@mark-anders
Copy link

Hi @Jtango18 , check out the thread for #426, because I think it's the same thing and the responses to my question outline how things execute. In essence, the code does not run as it reads.

@modocache
Copy link
Member

Yup, thanks for the tip, @mark-anders! Awesome to see you help diagnose these issues. 💯

@Jtango18 you'll need to wrap these two lines in a beforeEach:

beforeEach {
    let segment = TripSegment(coordinates: [CLLocationCoordinate2D()], distance: 37.5);
    trip.addSegment(segment)
}

If after reading the above and #426 you still have questions, post them here! 👍

@vinamelody
Copy link

vinamelody commented Nov 15, 2016

Hi, sorry to bump this topic again.. I'm trying to understand this same beforeEach in a global way.

My code looks like this:

class CurrencyConfiguration: QuickConfiguration {

    override class func configure(_ configuration: Configuration) {

        print("Begin configure...")

        configuration.beforeEach {
            print("Hey i'm inside configuration.beforeEach")
        }
    }
}

class CurrencyTest: QuickSpec {

    override func spec() {

        describe("Valid Currency") {

            beforeEach {
                print("This is beforeEach in QuickSpec")
            }
            print("Valid Currency is called here.")

        }
    }
}

The result log is:

Begin configure...
Valid Currency is called here.
Test Suite 'All tests' started at 2016-11-15 15:46:47.809
Test Suite 'Quick.framework' started at 2016-11-15 15:46:47.810
Test Suite 'Quick.framework' passed at 2016-11-15 15:46:47.812.
     Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Test Suite 'appTests.xctest' started at 2016-11-15 15:46:47.813
Test Suite 'appTests.xctest' passed at 2016-11-15 15:46:47.813.
     Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Test Suite 'All tests' passed at 2016-11-15 15:46:47.814.
     Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.005) seconds


Having wrapped the line with beforeEach, why is the global configuration.beforeEach not called?
I mean, these 2 lines are not printed:
print("Hey i'm inside configuration.beforeEach") and print("This is beforeEach in QuickSpec")

Thanks for your help!

@jeffh
Copy link
Member

jeffh commented Nov 15, 2016

Hey @vinamelody,

beforeEach only executes if there's an it block associated with it to run with.

class CurrencyTest: QuickSpec {

    override func spec() {

        describe("Valid Currency") {

            beforeEach {
                print("This is beforeEach in QuickSpec")
            }
            print("Valid Currency is called here.")

            //
            // This line below will cause beforeEaches to fire
            //
            it("does something") { }
        }
    }
}

@vinamelody
Copy link

@jeffh awesome! that works :) thank you!

@jeffh
Copy link
Member

jeffh commented Nov 30, 2016

No problem! 😄

@mykoma
Copy link

mykoma commented Nov 3, 2020

Hey @vinamelody,

beforeEach only executes if there's an it block associated with it to run with.

class CurrencyTest: QuickSpec {

    override func spec() {

        describe("Valid Currency") {

            beforeEach {
                print("This is beforeEach in QuickSpec")
            }
            print("Valid Currency is called here.")

            //
            // This line below will cause beforeEaches to fire
            //
            it("does something") { }
        }
    }
}

Its behavior is a little strange to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants