有没有更好的方式在摩卡书写testing语句?

在下面的代码中,我试图确保在用户单击链接之后呈现某个页面。 我不知道这是否是正确的方式来做出这个断言,或者如果有更好的方法来写它,也许与另一个内在的声明。 这个testing能以任何方式改进吗?

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(); }); }); }); }); 

  1. 在BDD风格中, describe()应该描述要testing的东西 。 你会想要分离每个页面访问每个断言。
  2. 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()套件来描述页面元素,例如“联系人链接”,尽pipeit()不能嵌套。