diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 21:46:22 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 21:46:22 -0500 |
commit | a065e04d529da1d847b5062a12c46d916408bf32 (patch) | |
tree | fe0f8bcec1ff39a3c499a2708222dcf15224ff70 /files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html | |
parent | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (diff) | |
download | translated-content-a065e04d529da1d847b5062a12c46d916408bf32.tar.gz translated-content-a065e04d529da1d847b5062a12c46d916408bf32.tar.bz2 translated-content-a065e04d529da1d847b5062a12c46d916408bf32.zip |
update based on https://github.com/mdn/yari/issues/2028
Diffstat (limited to 'files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html')
-rw-r--r-- | files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html | 283 |
1 files changed, 0 insertions, 283 deletions
diff --git a/files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html b/files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html deleted file mode 100644 index 5f7537ec42..0000000000 --- a/files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html +++ /dev/null @@ -1,283 +0,0 @@ ---- -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 ---- -<p>{{LegacyAddonsNotice}}{{AddonSidebar}}</p> - -<div class="note"> -<p>Unstable</p> -</div> - -<p>实现在 <a href="http://wiki.commonjs.org/wiki/Unit_Testing/1.1">CommonJS Unit Testing specification version 1.1</a> 其中定义的断言接口。</p> - -<h2 id="Usage">Usage</h2> - -<p>To use the <code>assert</code> module, write a set of unit tests following the instructions in the <a href="/en-US/Add-ons/SDK/Tutorials/Unit_testing">unit testing tutorial</a>. Each test will be passed an <code>Assert</code> object when you run the tests using <a href="/en-US/Add-ons/SDK/Tools/jpm"><code>jpm test</code></a>. You can use this object to make assertions about your program's state.</p> - -<p>For example:</p> - -<pre class="brush: js">var a = 1; - -exports["test value of a"] = function(assert) { - assert.ok(a == 1, "test that a is 1"); -} - -require("sdk/test").run(exports);</pre> - -<h2 id="Globals">Globals</h2> - -<h3 id="Constructors">Constructors</h3> - -<h4 class="addon-sdk-api-name" id="Assert(logger)"><code>Assert(logger)</code></h4> - -<p>Create a new Assert object. This function is only called by the unit test framework, and not by unit tests themselves.</p> - -<h5 id="Parameters">Parameters</h5> - -<p><strong>logger : object</strong><br> - Object used to log the results of assertions.</p> - -<h2 id="Assert">Assert</h2> - -<p>An object used to make assertions about a program's state in order to implement unit tests.</p> - -<p>The <code>Assert</code> object's interface is defined by the <a href="http://wiki.commonjs.org/wiki/Unit_Testing/1.1">CommonJS Unit Testing specification, version 1.1</a>.</p> - -<h3 id="Methods">Methods</h3> - -<h4 class="addon-sdk-api-name" id="ok(guard_message)"><code>ok(guard, message)</code></h4> - -<p>Tests whether an expression evaluates to true.</p> - -<pre class="brush: js">assert.ok(a == 1, "test that a is equal to one");</pre> - -<p>This is equivalent to:</p> - -<pre class="brush: js">assert.equal(a == 1, true, "test that a is equal to one");</pre> - -<h5 id="Parameters_2">Parameters</h5> - -<p><strong>guard : expression</strong><br> - The expression to evaluate.</p> - -<p><strong>message : string</strong><br> - Optional message to log, providing extra information about the test.</p> - -<h4 class="addon-sdk-api-name" id="equal(actual_expected_message)"><code>equal(actual, expected, message)</code></h4> - -<p>Tests shallow, coercive equality with <code>==</code>:</p> - -<pre class="brush: js">assert.equal(1, 1, "test that one is one");</pre> - -<h5 id="Parameters_3">Parameters</h5> - -<p><strong>actual : object</strong><br> - The actual result.</p> - -<p><strong>expected : object</strong><br> - The expected result.</p> - -<p><strong>message : string</strong><br> - Optional message to log, providing extra information about the test.</p> - -<h4 class="addon-sdk-api-name" id="notEqual(actual_expected_message)"><code>notEqual(actual, expected, message)</code></h4> - -<p>Tests that two objects are not equal, using <code>!=</code>:</p> - -<pre class="brush: js">assert.notEqual(1, 2, "test that one is not two");</pre> - -<h5 id="Parameters_4">Parameters</h5> - -<p><strong>actual : object</strong><br> - The actual result.</p> - -<p><strong>expected : object</strong><br> - The expected result.</p> - -<p><strong>message : string</strong><br> - Optional message to log, providing extra information about the test.</p> - -<h4 class="addon-sdk-api-name" id="deepEqual(actual_expected_message)"><code>deepEqual(actual, expected, message)</code></h4> - -<p>Tests that two objects have a deep equality relation. Deep equality is defined in the <a href="http://wiki.commonjs.org/wiki/Unit_Testing/1.0#Assert">CommonJS specification for Assert</a>, item 7, which is quoted here:</p> - -<div class="quote"> -<ol> - <li>All identical values are equivalent, as determined by ===.</li> - <li>If the expected value is a Date object, the actual value is equivalent if it is also a Date object that refers to the same time.</li> - <li>Other pairs that do not both pass typeof value == "object", equivalence is determined by ==.</li> - <li>For all other Object pairs, including Array objects, equivalence is determined by having the same number of owned properties (as verified with Object.prototype.hasOwnProperty.call), the same set of keys (although not necessarily the same order), equivalent values for every corresponding key, and an identical "prototype" property. Note: this accounts for both named and indexed properties on Arrays.</li> -</ol> -</div> - -<pre class="brush: js">assert.deepEqual({ a: "foo" }, { a: "foo" }, "equivalent objects");</pre> - -<h5 id="Parameters_5">Parameters</h5> - -<p><strong>actual : object</strong><br> - The actual result.</p> - -<p><strong>expected : object</strong><br> - The expected result.</p> - -<p><strong>message : string</strong><br> - Optional message to log, providing extra information about the test.</p> - -<h4 class="addon-sdk-api-name" id="notDeepEqual(actual_expected_message)"><code>notDeepEqual(actual, expected, message)</code></h4> - -<p>Tests that two objects do not have a deep equality relation, using the negation of the test for deep equality:</p> - -<pre class="brush: js">assert.notDeepEqual({ a: "foo" }, Object.create({ a: "foo" }), - "object's inherit from different prototypes");</pre> - -<h5 id="Parameters_6">Parameters</h5> - -<p><strong>actual : object</strong><br> - The actual result.</p> - -<p><strong>expected : object</strong><br> - The expected result.</p> - -<p><strong>message : string</strong><br> - Optional message to log, providing extra information about the test.</p> - -<h4 class="addon-sdk-api-name" id="strictEqual(actual_expected_message)"><code>strictEqual(actual, expected, message)</code></h4> - -<p>Tests that two objects are equal, using the strict equality operator <code>===</code>:</p> - -<pre class="brush: js">// 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'"); -}</pre> - -<h5 id="Parameters_7">Parameters</h5> - -<p><strong>actual : object</strong><br> - The actual result.</p> - -<p><strong>expected : object</strong><br> - The expected result.</p> - -<p><strong>message : string</strong><br> - Optional message to log, providing extra information about the test.</p> - -<h4 class="addon-sdk-api-name" id="notStrictEqual(actual_expected_message)"><code>notStrictEqual(actual, expected, message)</code></h4> - -<p>Tests that two objects are not equal, using the negation of the strict equality operator <code>===</code>:</p> - -<pre class="brush: js">// 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'"); -}</pre> - -<h5 id="Parameters_8">Parameters</h5> - -<p><strong>actual : object</strong><br> - The actual result.</p> - -<p><strong>expected : object</strong><br> - The expected result.</p> - -<p><strong>message : string</strong><br> - Optional message to log, providing extra information about the test.</p> - -<h4 class="addon-sdk-api-name" id="throws(block_error_message)"><code>throws(block, error, message)</code></h4> - -<p>Assert that a block of code throws the expected exception.</p> - -<p>This method takes an optional <code>Error</code> argument:</p> - -<ul> - <li> - <p>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.</p> - </li> - <li> - <p>to check that the exception thrown contains a specific message, pass a regular expression here: the <code>message</code> property of the exception thrown must match the regular expression</p> - </li> -</ul> - - -<p>For example, suppose we define two different custom exceptions:</p> - -<pre class="brush: js">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;</pre> - -<p>We can check the type of exception by passing a function as the <code>Error</code> argument:</p> - -<pre class="brush: js">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"); -}</pre> - -<p>We can check the message by passing a regular expression:</p> - -<pre class="brush: js">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"); -}</pre> - -<h5 id="Parameters_9">Parameters</h5> - -<p><strong>block : block</strong><br> - The block of code to test.</p> - -<p><strong>error : function|RegExp</strong><br> - Either a constructor function returning the type of exception expected, or a regular expression expected to match the exception's <code>message</code> property.</p> - -<p><strong>message : string</strong><br> - Optional message to log, providing extra information about the test.</p> |