Logo: Relish

  1. Sign in

Project: Dspec

Execution scope

The closures in the various methods get bound to a Context object, which is
cloned throughout the test run, in an attempt to share context where
appropriate, but allow each example a clean version of that shared context. This
is a last resort though, proper setup and tear down in beforeEach and afterEach
hooks should provide adequate isolation.


describe("Context", function() {

    $this->objA = new stdClass;

    it("has acccess to objA", function() {
        if (!isset($this->objA)) {
            throw new Exception("Could not access objA");

        $this->objB = new stdClass;

    it("does not have access to objB", function() {
        if (isset($this->objB)) {
            throw new Exception("Could access objB");

Using $this is totally optional, in PHP 5.3 you can use let and injection to pass vars around:


describe("test", function() {
    let("objA", function() {
        return new stdClass;

    it("has acccess to objA", function($objA) {
        assertThat($objA, anInstanceOf("stdClass"));

Using regular variable binding with your closures
is an alternative option, but can be a bit messier;


describe("Connection", function() {

    $db = new Connection();

    $dave = new User();
    $bex  = new User();

    beforeEach(function() use ($db, $dave, $bex) {

    context("#findAll()", function() use ($db) {
        it("responds with all records", function() use ($db) {
            $users = $db->find('users');
            assertThat(count($users), equalTo(2));



  1. Clean context per example
  2. Use let to define factories for context vars

Last published over 5 years ago by Dave Marshall.