【问题标题】:Jasmine tests in Livescript: "it" keyword conflictLivescript 中的 Jasmine 测试:“it”关键字冲突
【发布时间】:2015-10-16 18:20:31
【问题描述】:

我正在尝试将一些 Jasmine 测试从 javascript 移植到 LiveScript。 Jasmine 的功能之一是it。这是一个例子:

(function(){
  describe("The selling page", function() {
    // bla bla
    it("should display cards.", function() {
        expect(selectedCards.count()).toEqual(1);
    });
});

所以,我在 LiveScript 中尝试了这个:

do !->
  describe "the selling page " !->
   it "should display cards" !->
       expect (selectedCards.count()).toEqual("")

编译成这样:

// Generated by LiveScript 1.3.1
(function(){
   describe("the selling page ", function(it){ // <--- note the "it"
     it("should display cards", function(){
     expect(browser.getTitle().toEqual(""));
});
});
})();

您注意到第二个函数将“it”作为参数。我没有找到关于此的 livescript 文档,但我知道这是一个功能(如果我用其他东西替换 it 函数,那一切都很好。这让我确信我没有部分应用函数)。

但是,我需要 it 函数,我不希望它作为参数。那我怎么写这个sn-p呢?谢谢!

【问题讨论】:

    标签: livescript


    【解决方案1】:

    您应该在描述中使用 (...) 以避免添加额外的参数

    像这样:

    do !->
      describe "the selling page", (...) !->
       it "should display cards" !->
           expect (selectedCards.count()).toEqual("")
    

    【讨论】:

    • 这行得通,谢谢。嗯……所以我们在这里不使用 splats 来“使用当前函数的参数调用函数”。我们是否在这里对参数进行解构?
    • @Ehvince 这是一个匿名的 splat,它不会绑定任何东西
    【解决方案2】:

    如果您不介意将 it 别名为其他内容,您也可以这样做:

    that = it
    
    do !->
      describe "the selling page " !->
       that "should display cards" !->
           expect selectedCards.count! .to-equal ''
    

    it不在函数中时,它被视为全局it

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2014-10-06
      • 2015-06-30
      相关资源
      最近更新 更多