Test.Assertions

MVC.Test.Assertions, assert, assert_each, assert_equal, assert_not, assert_null, assert_not_null, pass, fail, next, next_callback

Related

Test, Test.Unit, Test.Functional, Test.Controller

Assertions run test functions, provide helpers, and record the results of the tests.

Example

this.assert_equal("Tiger", this.name, "Tiger was expected");
this.assert_not_null(this.title, "Title was null");
this.assert_null(this.obj, "Expected to be null");
this.assert(x_value > 200);

Constructor

MVC.Test.Assertions

new MVC.Test.Assertions(test, test_name) -> Assertion

Creates a new Assertion with the given test for the test that matches test_name.

- An instance of a MVC.Test class.
- A function name.

Prototype Methods

assert

assert(expression,message) -> undefined

Asserts the expression exists in the same way that if(expression) does. If the expression doesn't exist reports the error.

new MVC.Test.Unit('TestCase Name',{
  test_some_asserts : function(){
    this.assert(true)      //passes
    this.assert({})        //passes
    this.assert([])        //passes
    this.assert(7)         //passes
    this.assert(0)         //fails
    this.assert(false)     //fails
    this.assert('')        //fails
    this.assert(null)      //fails
    this.assert(undefined, 
         "Something was expected.") //fails
  }
)
- An expression
- An optional message. A default is provided if the message isn't present.

assert_each

assert_each(expected, actual, expression)  -> undefined

Asserts each element in the actual array matches each element in the expected array. If one element doesn't match, it fails.

this.assert_each([1,2,3], my_array, "Array not valid");
- The array of expected values
- The array to check for matching values
- An optional message. A default is provided if the message isn't present.

assert_equal

assert_equal(expected, actual, message) -> undefined

Uses the double equals (==) to determine if two values are equal. This means that type coercion may occur. For example 5 == '5'.

new MVC.Test.Unit('TestCase Name',{
  test_some_asserts : function(){
    this.assert_equal(7,7)      //passes
	this.assert_equal(7,'7')    //passes
    this.assert_equal('s','s')  //passes
    this.assert_equal(0,false)  //passes
    this.assert_equal("Tiger", this.name, "Tiger was expected");
    this.assert_equal(6,7)      //fails
  }
)
- The expected value
- The variable to check for or the value being checked
- An optional message. A default is provided if the message isn't present.

assert_null

assert_null(object, expression)  -> undefined

Passes if the given object == null. Fails otherwise.

this.assert_null(this.obj, "Expected to be null");
- The object to check for null
- An optional message. A default is provided if the message isn't present.

assert_not

assert_not(expression, message)  -> undefined

Passes if the expression is false, fails if it is true

this.assert_not(x_value == 200);
- An expression
- An optional message. A default is provided if the message isn't present.

assert_not_null

assert_not_null(object, message)  -> undefined

Passes if object is != null, fails otherwise

this.assert_not_null(obj);
- The object to check for null
- An optional message. A default is provided if the message isn't present.

pass

pass()  -> undefined

Adds to the assertions pass count.

fail

fail(message)  -> undefined

Adds to the assertions failure count with a message.

- An optional message. A default is provided if the message isn't present.

error

error(message)  -> undefined

Adds to the error count and adds the message to the assertions messages array.

- An optional message. A default is provided if the message isn't present.

next

next(params, delay, fname)  -> undefined

Calls the next function in the array after a certain delay. Used at the end of a test function after an asynchronous event has been initiated, such as an Ajax call or an animation.

test_open_directory: function(){
   // call the next function after a delay of 2 seconds
   this.next(this.DirectoryDblclick(2), 2, 'verify_open');
},
verify_open: function(){
   this.assert_equal(5, params.element.childNodes.length);
}
- Optional parameters. If provided, this is passed into the function specified by fname as its parameter.
- An optional delay, after which the specified function is called. The default is 0.5 seconds.
- An optional function name. If none is give, defaults to the name of the function sequentially next in the array of test functions.

next_callback

next_callback(fname, delay)  -> undefined

Calls the next function in the array after a certain delay. Used in conjunction with asynchronous functions that use callback functions, such as an Ajax call or the Drag event.

test_drag: function(){
    this.Drag($E('draggable'),{from: 'pointA', to: 'pointB', 
        duration: 2, callback: this.next_callback('done_dragging', 3)})
}, 
done_dragging : function(){
    this.assert_equal(1, $E('pointB').next().childNodes.length);
}
- An optional function name. If none is give, defaults to the name of the function sequentially next in the array of test functions.
- An optional delay, after which the specified function is called. The default is 0.5 seconds.