--- title: test/assert slug: Mozilla/Add-ons/SDK/Low-Level_APIs/test_assert translation_of: Archive/Add-ons/Add-on_SDK/Low-Level_APIs/test_assert ---
{{LegacyAddonsNotice}}{{AddonSidebar}}
Unstable
实现在 CommonJS Unit Testing specification version 1.1 其中定义的断言接口。
To use the assert
module, write a set of unit tests following the instructions in the unit testing tutorial. Each test will be passed an Assert
object when you run the tests using jpm test
. You can use this object to make assertions about your program's state.
For example:
var a = 1; exports["test value of a"] = function(assert) { assert.ok(a == 1, "test that a is 1"); } require("sdk/test").run(exports);
Assert(logger)
Create a new Assert object. This function is only called by the unit test framework, and not by unit tests themselves.
logger : object
Object used to log the results of assertions.
An object used to make assertions about a program's state in order to implement unit tests.
The Assert
object's interface is defined by the CommonJS Unit Testing specification, version 1.1.
ok(guard, message)
Tests whether an expression evaluates to true.
assert.ok(a == 1, "test that a is equal to one");
This is equivalent to:
assert.equal(a == 1, true, "test that a is equal to one");
guard : expression
The expression to evaluate.
message : string
Optional message to log, providing extra information about the test.
equal(actual, expected, message)
Tests shallow, coercive equality with ==
:
assert.equal(1, 1, "test that one is one");
actual : object
The actual result.
expected : object
The expected result.
message : string
Optional message to log, providing extra information about the test.
notEqual(actual, expected, message)
Tests that two objects are not equal, using !=
:
assert.notEqual(1, 2, "test that one is not two");
actual : object
The actual result.
expected : object
The expected result.
message : string
Optional message to log, providing extra information about the test.
deepEqual(actual, expected, message)
Tests that two objects have a deep equality relation. Deep equality is defined in the CommonJS specification for Assert, item 7, which is quoted here:
assert.deepEqual({ a: "foo" }, { a: "foo" }, "equivalent objects");
actual : object
The actual result.
expected : object
The expected result.
message : string
Optional message to log, providing extra information about the test.
notDeepEqual(actual, expected, message)
Tests that two objects do not have a deep equality relation, using the negation of the test for deep equality:
assert.notDeepEqual({ a: "foo" }, Object.create({ a: "foo" }), "object's inherit from different prototypes");
actual : object
The actual result.
expected : object
The expected result.
message : string
Optional message to log, providing extra information about the test.
strictEqual(actual, expected, message)
Tests that two objects are equal, using the strict equality operator ===
:
// This test will pass, because "==" will perform type conversion exports["test coercive equality"] = function(assert) { assert.equal(1, "1", "test coercive equality between 1 and '1'"); } // This test will fail, because the types are different exports["test strict equality"] = function(assert) { assert.strictEqual(1, "1", "test strict equality between 1 and '1'"); }
actual : object
The actual result.
expected : object
The expected result.
message : string
Optional message to log, providing extra information about the test.
notStrictEqual(actual, expected, message)
Tests that two objects are not equal, using the negation of the strict equality operator ===
:
// This test will fail, because "==" will perform type conversion exports["test coercive equality"] = function(assert) { assert.notEqual(1, "1", "test coercive equality between 1 and '1'"); } // This test will pass, because the types are different exports["test strict equality"] = function(assert) { assert.notStrictEqual(1, "1", "test strict equality between 1 and '1'"); }
actual : object
The actual result.
expected : object
The expected result.
message : string
Optional message to log, providing extra information about the test.
throws(block, error, message)
Assert that a block of code throws the expected exception.
This method takes an optional Error
argument:
to check that the exception thrown is of the expected type, pass a constructor function: the exception thrown must be an instance of the object returned by that function.
to check that the exception thrown contains a specific message, pass a regular expression here: the message
property of the exception thrown must match the regular expression
For example, suppose we define two different custom exceptions:
function MyError(message) { this.name = "MyError"; this.message = message || "Default Message"; } MyError.prototype = new Error(); MyError.prototype.constructor = MyError; function AnotherError(message) { this.name = "AnotherError"; this.message = message || "Default Message"; console.log(this.message); } AnotherError.prototype = new Error(); AnotherError.prototype.constructor = AnotherError;
We can check the type of exception by passing a function as the Error
argument:
exports["test exception type 1 expected to pass"] = function(assert) { assert.throws(function() { throw new MyError("custom message"); }, MyError, "test throwing a specific exception"); } exports["test exception type 2 expected to fail"] = function(assert) { assert.throws(function() { throw new MyError("custom message"); }, AnotherError, "test throwing a specific exception"); }
We can check the message by passing a regular expression:
exports["test exception message 1 expected to pass"] = function(assert) { assert.throws(function() { throw new MyError("custom message"); }, /custom message/, "test throwing a specific message"); } exports["test exception message 2 expected to pass"] = function(assert) { assert.throws(function() { throw new AnotherError("custom message"); }, /custom message/, "test throwing a specific exception"); } exports["test exception message 3 expected to fail"] = function(assert) { assert.throws(function() { throw new MyError("another message"); }, /custom message/, "test throwing a specific message"); }
block : block
The block of code to test.
error : function|RegExp
Either a constructor function returning the type of exception expected, or a regular expression expected to match the exception's message
property.
message : string
Optional message to log, providing extra information about the test.