ruby-on-rails - Rspec - 未定义的方法 'let'

标签 ruby-on-rails ruby rspec capybara

这是我的 rspec 文件:

require 'spec_helper'

describe "Birds" do
  before { visit birds_path }
  it "should have the right title" do
    expect(page).to have_content("Approved Birds")
  end
  it "should contain the bird's name, genus, species" do
    let(:bird) { FactoryGirl.create(:bird) }
    expect(page).to have_content("#{bird.name}")
    expect(page).to have_content("#{bird.genus}")
    expect(page).to have_content("#{bird.species}")
  end
end

当我运行它时,出现以下错误:

Failure/Error: let(:bird) { FactoryGirl.create(:bird) }
 NoMethodError:
   undefined method `let' for #<RSpec::Core::ExampleGroup::Nested_1:0xad87f84>

我的 Gemfile 包含“rspec-rails”和“capybara”gem。任何人都知道如何解决这个问题? 谢谢

最佳答案

describe "Birds" do
  let(:bird) { FactoryGirl.create(:bird) }

你不应该在 it block 中使用 let block 。它应该从 describe

中调用

关于ruby-on-rails - Rspec - 未定义的方法 'let',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23820224/

相关文章:

javascript - 在javascript中访问rails路由

ruby-on-rails - 我不明白 Ruby Proc 背后的想法......为什么不使用普通方法?

ruby-on-rails - 使用 Turbolinks 和 jQuery Turbolinks

ruby-on-rails - WebMock.disable_net_connect!不使用 rspec(没有 VCR)

RSpec:如何测试 boolean 返回类型?

html - Rails Image_Tag 无法访问其他目录中的图像

ruby-on-rails - Authlogic:如何在不知道密码的情况下在幕后登录用户

ruby - MacVim 命令窗口文本着色帮助(Rspec 输出)

ruby-on-rails - 如何使用 Rspec 在 Rails 中测试模型默认值

ruby-on-rails - 通过正则表达式解析字符串