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

Method calls and object initialization inside Quick 'describe' and 'context' blocks can be harmful #1781

Closed
ornithocoder opened this Issue Aug 16, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@ornithocoder
Contributor

ornithocoder commented Aug 16, 2017

Quick run its tests in a very specific (tree-based) order. Take the example below:

describe("MyClass") {
    // variable definition #1
    var sut: MyClass!
    var userDefaultsMock: UserDefaults!
 
    // beforeEach #1
    beforeEach { /* ... */ }
 
    // afterEach #1
    afterEach { /* ... */ }
 
    // it #1
    it("exists") { /* ... */ }
 
    // it #2
    it("conforms to the protocol ABC") { /* ... */ }
 
    describe("tableView") {
        // it #3
        it("exists") { /* ... */ }
 
        context("when user is logged out") {
            // beforeEach #2
            beforeEach { /* ... */ }
 
            // it #4
            it("has the correct background color") { /* ... */ }
        });
 
        context("when user is logged in") {
            context("and has used Facebook login") {
                // beforeEach #3
                beforeEach { /* ... */ }
 
                // it #5
                it("has the correct background color") { /* ... */ }
            });
 
            context("and has used Twitter login") {
                // beforeEach #4
                beforeEach { /* ... */ }
                 
                // it #6
                it("has the correct background color") { /* ... */ }
            }
        }
    }
}

The order of execution is:

  1. Setup
    • variable definition # 1
  2. Test 1
    • beforeEach # 1
    • it # 1
    • afterEach # 1
  3. Test 2
    • beforeEach # 1
    • it # 2
    • afterEach # 1
  4. Test 3
    • beforeEach # 1
    • it # 3
    • afterEach # 1
  5. Test 4
    • beforeEach # 1
    • beforeEach # 2
    • it # 4
    • afterEach # 1
  6. Test 5
    • beforeEach # 1
    • beforeEach # 3
    • it # 5
    • afterEach # 1
  7. Test 6
    • beforeEach # 1
    • beforeEach # 4
    • it # 6
    • afterEach # 1

Variable definition isn't called again between tests and, for this reason, variable initialization should happen inside beforeEach blocks to avoid object state to be carried along the test file.

When the variable initialization happens outside a beforeEach and a test changes the object state, this will persist and might influence other tests. In other words, the following should be avoided:

describe("MyClass") {
    let sut = MyClass()
    let userDefaultsMock = UserDefaultsMock()
 
    beforeEach { /* ... */ }
 
    it("does something") { /* ... */ }
 
    it("does something else") { /* ... */ }
}

rule request, opt-in, no calls inside describe and context blocks.

@ornithocoder ornithocoder changed the title from Method calls and object initialization can be harmful inside Quick 'describe' and 'context' blocks to Method calls and object initialization inside Quick 'describe' and 'context' blocks can be harmful Aug 16, 2017

ornithocoder added a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Aug 16, 2017

Add quick_discouraged_call opt-in rule
Implements realm#1781 (Method calls and object initialization inside Quick
'describe' and 'context' blocks can be harmful)

ornithocoder added a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Aug 16, 2017

Add quick_discouraged_call opt-in rule
Implements realm#1781 (Method calls and object initialization inside Quick
'describe' and 'context' blocks can be harmful)

ornithocoder added a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Aug 18, 2017

Add quick_discouraged_call opt-in rule
Implements realm#1781 (Method calls and object initialization inside Quick
'describe' and 'context' blocks can be harmful)
@ornithocoder

This comment has been minimized.

Show comment
Hide comment
@ornithocoder

ornithocoder Aug 18, 2017

Contributor

Merged in #1782. Thanks @marcelofabri! :D

Contributor

ornithocoder commented Aug 18, 2017

Merged in #1782. Thanks @marcelofabri! :D

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