Struct rspec::block::context::Context[][src]

pub struct Context<T> { /* fields omitted */ }

Test contexts are a convenient tool for adding structure and code sharing to a test suite.

Implementations

impl<T> Context<T>[src]

pub fn num_blocks(&self) -> usize[src]

pub fn num_examples(&self) -> usize[src]

pub fn is_empty(&self) -> bool[src]

impl<T> Context<T> where
    T: Clone
[src]

pub fn context<F>(&mut self, name: &'static str, body: F) where
    F: FnOnce(&mut Context<T>),
    T: Debug
[src]

Open and name a new context within the current context.

Note that the order of execution IS NOT guaranteed to match the declaration order.

Examples

runner.run(&rspec::suite("a test suite", (), |ctx| {
    ctx.context("opens a context labeled 'context'", |_ctx| {
        // …
    });
}));

Corresponding console output:

tests:
Suite "a test suite":
    Context "opens a sub-context":
        …

Available aliases:

pub fn specify<F>(&mut self, name: &'static str, body: F) where
    F: FnOnce(&mut Context<T>),
    T: Debug
[src]

Alias for context, see for more info.

Available further aliases:

pub fn when<F>(&mut self, name: &'static str, body: F) where
    F: FnOnce(&mut Context<T>),
    T: Debug
[src]

Alias for context, see for more info.

Available further aliases:

pub fn scope<F>(&mut self, body: F) where
    F: FnOnce(&mut Context<T>),
    T: Debug
[src]

Open a new name-less context within the current context which won't show up in the logs.

This can be useful for adding additional structure (and before/after blocks) to your tests without their labels showing up as noise in the console output. As such one might want to selectively assign two contexts/examples an additional before block without them getting visually separated from their neighboring contexts/examples.

Examples

runner.run(&rspec::suite("a suite",(), |ctx| {
    ctx.context("a context", |ctx| {
        ctx.scope(|ctx| {
            ctx.example("an example", |_env| {
                // …
            });
        });
    });
}));

Corresponding console output:

tests:
Suite "a suite":
    Context "a context":
        Example "an example"

The before_each(…) block gets executed before 'It "tests a"' and 'It "tests a"', but not before 'It "tests c"'.

pub fn example<F, U>(&mut self, name: &'static str, body: F) where
    F: 'static + Fn(&T) -> U,
    U: Into<ExampleResult>, 
[src]

Open and name a new example within the current context.

Note that the order of execution IS NOT guaranteed to match the declaration order.

Examples

runner.run(&rspec::suite("a test suite", (), |ctx| {
    ctx.example("an example", |_env| {
        // …
    });
}));

Corresponding console output:

tests:
Suite "a test suite":
    Example "an example":
        …

Available aliases:

pub fn it<F, U>(&mut self, name: &'static str, body: F) where
    F: 'static + Fn(&T) -> U,
    U: Into<ExampleResult>, 
[src]

Alias for example, see for more info.

Available further aliases:

pub fn then<F, U>(&mut self, name: &'static str, body: F) where
    F: 'static + Fn(&T) -> U,
    U: Into<ExampleResult>, 
[src]

Alias for example, see for more info.

Available further aliases:

pub fn before_all<F>(&mut self, body: F) where
    F: 'static + Fn(&mut T), 
[src]

Declares a closure that will be executed once before any of the context's children (context or example blocks) are being executed.

Note that the order of execution IS NOT guaranteed to match the declaration order.

Examples

runner.run(&rspec::suite("a test suite", (), |ctx| {
    ctx.before_all(|_env| {
        // …
    });

    ctx.example("an example", |_env| {
        // …
    });

    ctx.example("another example", |_env| {
        // …
    });
}));

Corresponding console output:

tests:
Suite "a test suite":
    Example "an example":
        …
    Example "another example":
        …

Available aliases:

pub fn before<F>(&mut self, body: F) where
    F: 'static + Fn(&mut T), 
[src]

Alias for before_all, see for more info.

pub fn before_each<F>(&mut self, body: F) where
    F: 'static + Fn(&mut T), 
[src]

Declares a closure that will be executed once before each of the context's children (context or example blocks).

Note that the order of execution IS NOT guaranteed to match the declaration order.

Examples

runner.run(&rspec::suite("a test suite", (), |ctx| {
    ctx.before_each(|_env| {
        // …
    });

    ctx.example("an example", |_env| {
        // …
    });

    ctx.example("another example", |_env| {
        // …
    });
}));

Corresponding console output:

tests:
Suite "a test suite":
    Example "an example":
        …
    Example "another example":
        …

pub fn after_all<F>(&mut self, body: F) where
    F: 'static + Fn(&mut T), 
[src]

Declares a closure that will be executed once after any of the context's children (context or example blocks) have been executed.

Note that the order of execution IS NOT guaranteed to match the declaration order.

Examples

runner.run(&rspec::suite("a test suite", (), |ctx| {
    ctx.after_all(|_env| {
        // …
    });

    ctx.example("an example", |_env| {
        // …
    });

    ctx.example("another example", |_env| {
        // …
    });
}));

Corresponding console output:

tests:
Suite "a test suite":
    Example "an example":
        …
    Example "another example":
        …

Available aliases:

pub fn after<F>(&mut self, body: F) where
    F: 'static + Fn(&mut T), 
[src]

Alias for after_all, see for more info.

pub fn after_each<F>(&mut self, body: F) where
    F: 'static + Fn(&mut T), 
[src]

Declares a closure that will be executed once after each of the context's children (context or example blocks).

Note that the order of execution IS NOT guaranteed to match the declaration order.

Examples

runner.run(&rspec::suite("a test suite", (), |ctx| {
    ctx.after_each(|_env| {
        // …
    });

    ctx.example("an example", |_env| {
        // …
    });

    ctx.example("another example", |_env| {
        // …
    });
}));

Corresponding console output:

tests:
Suite "a test suite":
    Example "an example":
        …
    Example "another example":
        …

Trait Implementations

impl<T> Send for Context<T> where
    T: Send
[src]

impl<T> Sync for Context<T> where
    T: Sync
[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for Context<T>[src]

impl<T> Unpin for Context<T>[src]

impl<T> !UnwindSafe for Context<T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pointable for T

type Init = T

The type for initializers.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.