EmberJS 验收测试
验收测试
为了测试用户与应用程序和应用程序的交互,使用了验收测试。通过使用验收测试,用户可以通过登录表单登录,并可以创建博客帖子。
例子
<!DOCTYPE html>
<html>
<head>
<title>Emberjs Acceptance Test</title>
<!-- CDN's-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.prod.js"></script>
<script src="https://builds.emberjs.com/release/ember.debug.js"></script>
<script src="https://builds.emberjs.com/beta/ember-data.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="ember-testing-container"><div id="ember-testing"></div></div>
<script type="text/x-handlebars" data-template-name="index">
<h2>Names are:</h2>
<ul>
{{#each}}
<li>{{#link-to 'post' this}} {{firstName}} {{lastName}} {{/link-to}}</li>
{{/each}}
</ul>
</script>
<script type="text/x-handlebars" data-template-name="post">
<h2>Name :</h2>
<p id='name'>
{{firstName}}
</p>
</script>
<script type="text/javascript">
//Creates an instance of Ember.Application and assign it to a global variable
App = Ember.Application.create();
//The store cache of all records available in an application
App.Store = DS.Store.extend({
//adapter translating requested records into the appropriate calls
adapter: DS.FixtureAdapter
});
App.Router.map(function() {
//post route
this.route('post', { path: ':person_id' });
});
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('person');
}
});
App.Person = DS.Model.extend({
//setting firstName and lastName attributes as string
firstName: DS.attr('string'),
lastName: DS.attr('string')
});
//attach fixtures(sample data) to the model's class
App.Person.FIXTURES = [
{id: 1, firstName: 'John', lastName: 'Smith'},
{id: 2, firstName: 'Tom', lastName: 'Cruise'},
{id: 3, firstName: 'Shane', lastName: 'Watson'}
];
App.rootElement = '#ember-testing'; //Ember.js applications's root element
//These two methods to prepare the application for testing
App.setupForTesting();
App.injectTestHelpers();
module("Emberjs", {
setup: function() {
Ember.run(App, App.advanceReadiness); //'advanceReadiness' runs the application when not under test
},
//This function is called for each test in the module
teardown: function() {
App.reset(); //After each test, reset the state of the application
}
});
//Here, it tests the workflow of application
test("Check HTML is returned", function() {
visit("/").then(function() {
ok(exists("*"), "Found HTML!");
});
});
</script>
</body>
</html>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将上面的代码保存在emberjs-testing-acceptance_test.html文件中
在浏览器中打开此HTML文件。

EmberJS 测试