English 中文(简体)
如何测试功能, 取决于在 Jasmine 单位测试中其他对象
原标题:How to test function depending on other objects in jasmine unit test

我要对一个叫"事件"的骨干模型 进行一个茉莉乳细胞测试

在这个模型中,我有一个功能:

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

它有4种依赖关系:

  • User model
  • Users collection
  • Participation model
  • Participations collections

我想孤立地测试所有模型,因为这是最佳做法,但我不知道如何进行。

我用sinon.js 来嘲弄和嘲弄, 但不知道在这种情况下如何正确使用它。

谢谢!

最佳回答

您的方法 < code> getParticipent 正在做很多事情。 实际上有 3 个事情。 所以, 我宁可尝试让它只关注一件事: 构建 < code> Myappp. collectives. Users Collection 。 并且将所有先前需要的计算都移到其他方法 。

然后,你就可以集中关注非常棘手的问题: 模拟 new call 。

我分三个阶段做:

1. Move the Users filter to another place

我建议将其移动到 Myapp. collectives. participation Collection 中的一种方法。 参与集合 , 然后我们可以从我们的 < code> 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;
    }
  });

这样,我们就可以轻松地进行 < em> 模拟 < code> gigewuses 方法,测试我们的目标方法。

2. Wrapping, even more, the Users Array obtaining

I 上面的代码示例中, 我们仍有一个复杂的链条方法, 在行 this.get ("参与")。 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. Test the Collection initialization

现在我们必须集中力量模拟收藏 Myapp. collections. Users Collection , 并检查我们初始化方法, 以及本收藏实体中适当的参数 。

proper prams 是方法 Event.getUsers Array () ()

Sinon特别希望检查是否使用 new 的方法。

我的茉莉花/西尼翁方法并不准确, 因为我没有测试该方法是否正在返回 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();
  });
});

"http://jsfiddle.net/fguillen/J5WTf/" rel="nofollow" > check the jsfiddle

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签