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

Add example of overriding in nested contexts? #17

Open
ms-ati opened this issue Nov 23, 2018 · 1 comment
Open

Add example of overriding in nested contexts? #17

ms-ati opened this issue Nov 23, 2018 · 1 comment

Comments

@ms-ati
Copy link
Contributor

ms-ati commented Nov 23, 2018

Do you think something like this should go in an example in the README? Or update the example spec to include this overriding of values in nested contexts?

I only ask because this a super-useful pattern for re-using test setup with many variations across your specs, and it took me quite a while to figure out exactly how to get this to work, even after re-reading our last exchange on issue #5.

#[cfg(test)]
extern crate speculate;

#[cfg(test)]
use speculate::speculate;

#[macro_use]
extern crate lazy_static;

fn add(a: i32, b: i32) -> i32 {
    a + b
}

speculate! {
    describe "add" {
        lazy_static! { static ref A: i32 = { 1 }; }
        lazy_static! { static ref B: i32 = { 1 }; }
        before { lazy_static! { static ref SUBJECT: i32 = { add(*A, *B) }; } }

        context "when 1 and 1" {
            it "equals 2" {
                assert_eq!(*SUBJECT, 2);
            }
        }

        context "when B is 2" {
            lazy_static! { static ref B: i32 = { 2 }; }

            it "equals 3" {
                assert_eq!(*SUBJECT, 3);
            }

            context "and A is B + 1" {
                lazy_static! { static ref A: i32 = { *B + 1 }; }

                it "equals 5" {
                    assert_eq!(*SUBJECT, 5);
                }
            }
        }
    }
}
@ms-ati
Copy link
Contributor Author

ms-ati commented Nov 23, 2018

But the following does NOT work, because the lazy_static! for A does not update to take into account the most inner nested B -- any idea how to get this to work as intended in the innermost scope so that SUBJECT will end up as 9?

#[cfg(test)]
extern crate speculate;

#[cfg(test)]
use speculate::speculate;

#[macro_use]
extern crate lazy_static;

fn add(a: i32, b: i32) -> i32 {
    a + b
}

speculate! {
    describe "add" {
        lazy_static! { static ref A: i32 = { 1 }; }
        lazy_static! { static ref B: i32 = { 1 }; }
        before { lazy_static! { static ref SUBJECT: i32 = { add(*A, *B) }; } }

        context "when 1 and 1" {
            it "equals 2" {
                assert_eq!(*SUBJECT, 2);
            }
        }

        context "when B is 2" {
            lazy_static! { static ref B: i32 = { 2 }; }

            it "equals 3" {
                assert_eq!(*SUBJECT, 3);
            }

            context "and A is B + 1" {
                lazy_static! { static ref A: i32 = { *B + 1 }; }

                it "equals 5" {
                    assert_eq!(*SUBJECT, 5);
                }

                context "and B is doubled" {
                    lazy_static! { static ref B: i32 = { *super::B * 2 }; }

                    // Note this example of nested overrides!
                    //   * B is 4 (= 2 * 2)
                    //   * A is 5 (= 4 + 1)
                    // ...therefore, SUBJECT should be 9 here
                    it "equals 9" {
                        assert_eq!(*SUBJECT, 9);
                    }
                }
            }
        }
    }
}

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

No branches or pull requests

1 participant