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 · 6 comments

Comments

Projects
None yet
6 participants
@Jtango18

Jtango18 commented Dec 18, 2015

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

This comment has been minimized.

mark-anders commented Dec 18, 2015

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

This comment has been minimized.

Member

modocache commented Dec 18, 2015

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! 👍

@modocache modocache closed this Dec 18, 2015

@vinamelody

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

vinamelody commented Nov 15, 2016

@jeffh awesome! that works :) thank you!

@jeffh

This comment has been minimized.

Member

jeffh commented Nov 30, 2016

No problem! 😄

@istx25 istx25 added the question label Nov 30, 2016

@wongzigii wongzigii referenced this issue May 9, 2018

Open

Running code before each context #788

1 of 1 task complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment