【问题标题】:How to test function depending on other objects in jasmine unit test如何在茉莉花单元测试中根据其他对象测试功能
【发布时间】:2012-05-31 19:01:37
【问题描述】:

我有一个名为 Event 的主干模型的 jasmine 单元测试。

在这个模型中我有一个函数:

getParticipants: function(){
  new Myappp.Collections.UsersCollection(
    this.get("participations").map(function(participation){
      return participation.get("user");
    });
  );
}

它有 4 个依赖项:

  • 用户模型
  • 用户收藏
  • 参与模式
  • 参与合集

我想单独测试所有模型,因为这是最佳实践,但我不确定如何。

我正在使用 sinon.js 进行模拟和存根,但不知道在这种情况下如何正确使用它。

谢谢!

【问题讨论】:

    标签: javascript unit-testing backbone.js jasmine sinon


    【解决方案1】:

    您的方法getParticipants 做了很多事情.. 实际上是三件事,所以我宁愿尝试让它专注于一件事:构建Myappp.Collections.UsersCollection。并将所有以前需要的计算移至其他方法。

    然后您可以专注于非常棘手的问题:模拟new 调用

    我会分三个阶段进行:

    1。将用户过滤器移至其他位置

    我建议将它移到Myapp.Collections.ParticipationsCollection 中的一个方法中,然后我们可以像这样从我们的Event 实例中调用它:

    Myapp.Models.Event = 
      Backbone.Model.extend({
        getParticipants: function(){
          var usersArray   = this.get("participations").getUsers();
          var participants = new Myapp.Collections.UsersCollection(usersArray);
    
          return participants;
        }
      });
    
    Myapp.Collections.ParticipationsCollection = 
      Backbone.Collection.extend({
        getUsers: function(){
          var users = 
            this.map(function(participation){
              return participation.get("user");
            });
    
          return users;
        }
      });
    

    这样我们可以轻松地模拟getUsers 方法来测试我们的目标方法。

    2。包装,甚至更多,用户数组获得

    在上面的代码示例中,我们在this.get("participations").getUsers() 行中仍然有一个复杂的链方法调用,我们可以将其移至自定义方法,也易于模拟:

    我们以这样的方式结束:

    Myapp.Models.Event = 
      Backbone.Model.extend({
        getParticipants: function(){
          var usersArray   = this.getUsersArray();
          var participants = new Myapp.Collections.UsersCollection( usersArray );
    
          return participants;
        },
    
        getUsersArray: function(){
          return this.get("participations").getUsers();
        }
      });
    

    3。测试集合初始化

    现在我们必须专注于模拟 Collection Myapp.Collections.UsersCollection 并检查我们的初始化方法和这个 Collection 的实体是否具有适当的参数。

    正确的参数Event.getUsersArray()方法的结果,现在我们可以轻松地模拟它了。

    Sinon 有一个特别的期望,就是检查一个方法是否被 new.. 调用过。

    我的 Jasmine/Sinon 方法并不准确,因为我没有测试该方法是否返回 new 集合,我希望有人能提供更好的实现:

    describe("Event", function() {
      beforeEach(function(){
        testModel = new Myapp.Models.Event();
      });
    
      it("getParticipants returns proper Collection", function() {
        var usersCollectionSpy = sinon.spy( Myapp.Collections, "UsersCollection" );
    
        var getUsersArrayMock = sinon.stub(testModel, "getUsersArray");
        getUsersArrayMock.returns( "usersArray" );
    
        testModel.getParticipants();
    
        expect( usersCollectionSpy.calledWith( "usersArray" ) ).toBeTruthy();
        expect( usersCollectionSpy.calledWithNew() ).toBeTruthy();
      });
    });
    

    Check the jsFiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多