【问题标题】:Test model persistence in Ember.js acceptance testEmber.js 验收测试中的测试模型持久性
【发布时间】:2015-11-14 12:38:43
【问题描述】:

我正在尝试编写验收测试,以验证我的注册表单是否将 User 模型保存到商店。

我正在使用 Ember 1.13.6、Mocha、Chai 和 Ember CLI Mirage(伪造后端进行测试。)后端是 JSONAPI。

我对 ember 很陌生,并且在寻找测试策略方面有点挣扎。

我可以使用 Sinon.js 并监视模型并检查是否调用了 .save 方法,还是应该测试是否发送了正确的 XHR 请求 (POST /api/vi/users)?

// app/routes/users/new.js
import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
    return this.store.createRecord('user');
  },
  actions: {
    registerUser: function(){
      var user = this.model();
      user.save().then(()=> {
        this.transitionTo('/');
      }).catch(()=> {
        console.log('user save failed.');
      });
    }
  }
});

<!-- app/templates/users/new.hbs -->
<form {{ action "registerUser" on="submit" }}>
  <div class="row email">
    <label>Email</label>
    {{ input type="email" name="email" value=email }}
  </div>
  <div class="row password">
    <label>Password</label>
    {{ input type="password" name="password" value=password }}
  </div>
  <div class="row password_confirmation">
    <label>Password Confirmation</label>
    {{ input type="password" name="password_confirmation" value=password_confirmation }}
  </div>
  <button type="submit">Register</button>
</form>

/* jshint expr:true */
import {
  describe,
  it,
  beforeEach,
  afterEach
} from "mocha";
import { expect } from "chai";
import Ember from "ember";
import startApp from "tagged/tests/helpers/start-app";

describe("Acceptance: UsersNew", function() {
  var application;

  function find_input(name){
    return find(`input[name="${name}"]`);
  }

  beforeEach(function() {
    application = startApp();
    visit("/users/new");
  });

  afterEach(function() {
    Ember.run(application, "destroy");
  });

  describe("form submission", function(){

    const submit = function(){
        click('button:contains(Register)');
    };

    beforeEach(function(){
      fillIn('input[name="email"]', 'test@example.com');
      fillIn('input[name="password"]', 'p4ssword');
      fillIn('input[name="password_confirmation"]', 'p4ssword');
    });

    it("redirects to root path", function(){
      submit();
      andThen(function() {
        expect(currentURL()).to.eq('/'); // pass
      });
    });

    it("persists the user record", function(){
      // this is the part I am struggling with
      // expect user.save() to be called.
      submit();
    });
  });
});

【问题讨论】:

    标签: javascript ember.js ember-data mocha.js ember-cli-mirage


    【解决方案1】:

    您可以只使用 Mirage 来确保发送 xhr 请求。 server 是您的测试中可用的全局变量,它引用您的 Mirage 服务器。所以你可以这样做:

    server.post('/users', function(db, request) {
      let json = JSON.parse(request.requestBody);
      expect(json.email).to equal('test@example.com');
    });
    

    或任何 mocha 语法。确保您在开始时添加 expect(1) test to be run,因为您现在处于异步状态。


    OP 的最终解决方案:

    it('saves the user', function(){
      server.post('/users', function(db, request) {
        var json = JSON.parse(request.requestBody);
        var attrs = json['data']['attributes'];
        expect(attrs['email']).to.eq("test@example.com");
        expect(attrs['password']).to.eq("p4ssword");
        expect(attrs['password_confirmation']).to.eq("p4ssword");
      });
      click('button:contains(Register)');
    });
    

    【讨论】:

    • 感谢绝对为我指明了正确的方向。 Mocha 处理异步测试的方式与 QUnit 完全不同,因此您不必告诉它异步测试中有多少断言。这甚至无需创建 done 回调即可工作。
    猜你喜欢
    • 2019-08-12
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2013-03-13
    • 2022-01-14
    相关资源
    最近更新 更多