You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)]externcrate speculate;#[cfg(test)]use speculate::speculate;#[macro_use]externcrate lazy_static;fnadd(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);
}}}}}
The text was updated successfully, but these errors were encountered:
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)]externcrate speculate;#[cfg(test)]use speculate::speculate;#[macro_use]externcrate lazy_static;fnadd(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);
}}}}}}
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.
The text was updated successfully, but these errors were encountered: