pub struct Sequence { /* private fields */ }Expand description
Used to enforce that mock calls must happen in the sequence specified.
Sequences are performed greedily, and will try to use the earliest possible match.
§Examples
#[automock]
trait Foo {
fn foo(&self);
fn bar(&self) -> u32;
}
let mut seq = Sequence::new();
let mut mock0 = MockFoo::new();
let mut mock1 = MockFoo::new();
mock0.expect_foo()
.times(1)
.returning(|| ())
.in_sequence(&mut seq);
mock1.expect_bar()
.times(1)
.returning(|| 42)
.in_sequence(&mut seq);
mock0.foo();
mock1.bar();The count is allowed to vary.
#[automock]
trait Foo {
fn foo(&self);
fn bar(&self) -> u32;
}
let mut seq = Sequence::new();
let mut mock0 = MockFoo::new();
let mut mock1 = MockFoo::new();
mock0.expect_foo()
.times(1..4)
.returning(|| ())
.in_sequence(&mut seq);
mock1.expect_bar()
.times(1)
.returning(|| 42)
.in_sequence(&mut seq);
mock0.foo();
mock0.foo();
mock1.bar();But, the previous count must be satisfied before a sequence may make progress.
#[automock]
trait Foo {
fn foo(&self);
fn bar(&self) -> u32;
}
let mut seq = Sequence::new();
let mut mock0 = MockFoo::new();
let mut mock1 = MockFoo::new();
mock0.expect_foo()
.times(3..5)
.returning(|| ())
.in_sequence(&mut seq);
mock1.expect_bar()
.times(1)
.returning(|| 42)
.in_sequence(&mut seq);
mock0.foo();
mock0.foo();
mock1.bar();Furthermore, sequences are greedy, and will only perform the earliest
element in sequence that is allowed. This results in the following example
failing the second expectation, as the first expectation is used for both
calls to foo.
The following example fails as the first expection handles all calls,
leaving none for the second expectation. As the second expectation goes
unused, while it is expected to be called at least once, the test panics
with
MockFoo::foo: Expectation(<anything>) called 0 time(s) which is fewer than expected 1.
#[automock]
trait Foo {
fn foo(&self);
fn bar(&self) -> u32;
}
let mut seq = Sequence::new();
let mut mock0 = MockFoo::new();
mock0.expect_foo()
.returning(|| ())
.in_sequence(&mut seq);
mock0.expect_foo()
.times(1..)
.returning(|| ())
.in_sequence(&mut seq);
mock0.foo();
mock0.foo();