【问题标题】:Undefined method for Class in RspecRspec中类的未定义方法
【发布时间】:2011-08-23 08:28:13
【问题描述】:

以下 RSpec 2 测试..

describe "GET new" do
  describe "gets a report form" do
    xhr :get, :new, :post_id => @post
    response.should be_success
  end
end

给出了这个很好的错误:

undefined method xhr for #<Class:0xb5c72404> (NoMethodError)

知道有什么问题吗?

【问题讨论】:

  • 您的测试是否嵌套在 describe controller_name 中?
  • 是的,describe 块应该如何嵌套有特殊规则吗?
  • 你需要spec_helper吗?

标签: ruby-on-rails rspec rspec2


【解决方案1】:

事实证明,您必须在 describe 块中使用 it 语句。然后错误消失。如果你没有使用正确数量的describeit 块,那么RSpec 会产生各种奇怪的错误。这是正确的代码:

describe "GET new" do
  it "gets a report form" do
    xhr :get, :new, :post_id => @post
    response.should be_success
  end
end

【讨论】:

  • 该死,我没看到 :) +1
猜你喜欢
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多