有没有更好的方法在 mocha 中编写测试语句?
Is there a better way to write test statements in mocha?
在下面的代码中,我试图确保在用户单击 link 后呈现某个页面。我不确定这是否是做出此断言的正确方法,或者是否有更好的方法来编写它,也许使用另一个内部 it 语句。能否以任何方式改进此测试?
describe('User visits the main page and clicks the Contact link', function() {
const browser = new Browser();
it('should render ok', function(done) {
browser.visit( "http://localhost:3000",
function() {
browser.clickLink("Contact", function() {
browser.assert.text('title', 'Contact');
done();
});
});
});
});
- 在 BDD 风格中,
describe()
应该描述要测试的 事物 。您可能希望将每页的每个断言分开以进行访问。
browser.visit
不应在 it
语句内。将它放入 beforeEach
挂钩中。
describe('the main page', function() {
const browser = new Browser();
beforeEach(function(done) {
browser.visit("http://localhost:3000", done);
});
it('should render ok when user clicks the Contact link', function(done) {
browser.clickLink("Contact", function() {
browser.assert.text('title', 'Contact');
done();
});
});
it('another test', function(done) {
// goes here
});
});
您可以添加另一个嵌套 describe()
套件来描述页面元素,例如 "the Contact link," 尽管 it()
不能嵌套。
在下面的代码中,我试图确保在用户单击 link 后呈现某个页面。我不确定这是否是做出此断言的正确方法,或者是否有更好的方法来编写它,也许使用另一个内部 it 语句。能否以任何方式改进此测试?
describe('User visits the main page and clicks the Contact link', function() {
const browser = new Browser();
it('should render ok', function(done) {
browser.visit( "http://localhost:3000",
function() {
browser.clickLink("Contact", function() {
browser.assert.text('title', 'Contact');
done();
});
});
});
});
- 在 BDD 风格中,
describe()
应该描述要测试的 事物 。您可能希望将每页的每个断言分开以进行访问。 browser.visit
不应在it
语句内。将它放入beforeEach
挂钩中。
describe('the main page', function() {
const browser = new Browser();
beforeEach(function(done) {
browser.visit("http://localhost:3000", done);
});
it('should render ok when user clicks the Contact link', function(done) {
browser.clickLink("Contact", function() {
browser.assert.text('title', 'Contact');
done();
});
});
it('another test', function(done) {
// goes here
});
});
您可以添加另一个嵌套 describe()
套件来描述页面元素,例如 "the Contact link," 尽管 it()
不能嵌套。