Jasmine在测试之前调用

tsm*_*tsm 2 javascript unit-testing coffeescript jasmine

我正在写一个Rails应用程序,它具有以下茉莉花规格:

describe "buttons", ->

  beforeEach ->
    loadFixtures("foo.html")
    alert("beforeEach: " + $("tr.foo").length)

  describe ".hide_foo", ->
    alert(".hide-foo: " + $("tr.foo").length)
    ...
    expect($("tr.foo")).toBeHidden()
Run Code Online (Sandbox Code Playgroud)

规范因错误而失败:

TypeError: Cannot call method 'expect' of null
Run Code Online (Sandbox Code Playgroud)

所以我提出警报.首先我们看到".hide-foo:0",然后在我关闭之后"beforeEach:44"出现.很明显错误是因为我们在expect加载夹具之前调用了但是......为什么在每个例子之前没有beforeEach执行heck ?

我正在使用jasminerice来使用Rails Asset Pipeline来编译我的Coffeescript.版本:

$ bundle show jasmine && bundle show jasminerice
/home/tmacdonald/.rvm/gems/ruby-1.9.2-p320/gems/jasmine-1.2.0
/home/tmacdonald/.rvm/gems/ruby-1.9.2-p320/gems/jasminerice-0.0.9
Run Code Online (Sandbox Code Playgroud)

谢谢!

log*_*yth 6

describe块不应该直接在其中包含测试逻辑.您应该将所有测试逻辑放在一个it块中.

describe ".hide_foo", ->
    it 'should hide the row', ->
        alert(".hide-foo: " + $("tr.foo").length)

        expect($("tr.foo")).toBeHidden()
Run Code Online (Sandbox Code Playgroud)