FactoryGirl:`block(3级)in <top(required)=“”>':未定义的局部变量或方法`user'</top>

时间:2012-07-18 07:11:15

标签: ruby-on-rails rspec factory-bot

即使我user,我仍然会收到let(:user) { FactoryGirl.create(:user) }未定义的错误。我该如何解决这个问题?

我尝试删除,重新创建,迁移的数据库并执行了rake db:test:prepare

我也试过重启服务器。

这些技巧都没有成功。

forums_spec.rb

require 'spec_helper'

describe "Forums" do

  subject { page }

  shared_examples_for 'All signed off pages' do
    describe 'should have logo' do
      it { should have_selector('#logo', text: 'Minforum')}
    end

    describe 'should have navigation header' do
      it { should have_selector('.nav.pull-right li', text: 'Sign in') }
      it { should have_selector('.nav.pull-right li', text: 'Register') }
    end

    describe 'should have footer links' do
      it { should have_link('About', href: about_path) }
      it { should have_link('Code', href: 'https://github.com/serv/minforum') }
      it { should have_link('Privacy Policy', href: privacy_path) }
    end
  end

  shared_examples_for 'All pages' do
    it { should have_selector('h1', text: heading) }
    it { should have_selector('title', text: full_title(title)) }
  end

  describe 'index page' do
    before { visit root_path }
    let(:title) {''}
    let(:heading) {'Forums'}

    describe 'should have large intro display' do
      it { should have_selector('.intro h1', text: 'Minforum') }
      it { should have_selector('.intro p', text: 'Simple forum software written with Ruby on Rails') }
      it { should have_link('View project on GitHub', href: 'https://github.com/serv/minforum') }
    end

    describe 'should have valid table headings' do
      it { should have_selector('table thead tr th', text: '') }
      it { should have_selector('table thead tr th', text: 'Topics') }
      it { should have_selector('table thead tr th', text: 'Posts') }
      it { should have_selector('table thead tr th', text: 'Views') }
    end

    it_should_behave_like 'All pages'
    it_should_behave_like 'All signed off pages'
  end

  describe 'new page' do
    describe 'should not let visitors in' do
      before { visit new_forum_path }
      it { should have_selector('.alert.alert-notice', text:'You do not have permission to do that.') }
    end

    describe 'should not let nonadmin users in' do
      let(:user) { FactoryGirl.create(:user) }
      sign_in user
      visit new_forum_path

      it { should have_selector('.alert.alert-notice', text:'You do not have permission to do that.') }
      user.delete
    end
  end
end

factories.rb

FactoryGirl.define do
  factory :user do
    sequence(:name)  { |n| "Person#{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}   
    password "foobar"
    password_confirmation "foobar"
    bio 'This is a bio.'

    factory :admin do
      admin true
    end

    factory :mod do
      mod true
    end
  end

  factory :forum do
    name 'Jason Forum1'
    description 'The factory_girl gem is used to create default model objects for tests. For example, if a controller action requires finding a User object before displaying...'
    user
  end

  factory :topic do
    name 'The application generator template will ask you for your preferences'
    views 10
    forum_id 1
    last_post_id 10
  end
end

sign_in方法

def sign_in(user)
  visit signin_path
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
  # Sign in when not using Capybara as well.
  cookies[:remember_token] = user.remember_token
end

错误

Jasons-MacBook-Pro:minforum jasonkim$ bundle exec rspec spec/requests/forums_spec.rb 
/Users/jasonkim/rails/minforum/spec/requests/forums_spec.rb:59:in `block (3 levels) in <top (required)>': undefined local variable or method `user' for #<Class:0x007f95e7e64130> (NameError)
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:201:in `module_eval'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:201:in `subclass'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:187:in `describe'
    from /Users/jasonkim/rails/minforum/spec/requests/forums_spec.rb:57:in `block (2 levels) in <top (required)>'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:201:in `module_eval'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:201:in `subclass'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:187:in `describe'
    from /Users/jasonkim/rails/minforum/spec/requests/forums_spec.rb:51:in `block in <top (required)>'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:201:in `module_eval'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:201:in `subclass'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:187:in `describe'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/dsl.rb:18:in `describe'
    from /Users/jasonkim/rails/minforum/spec/requests/forums_spec.rb:3:in `<top (required)>'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `block in load_spec_files'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `map'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load_spec_files'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
    from /Users/jasonkim/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'
将答案更改为awendt

后出现

新错误

Jasons-MacBook-Pro:minforum jasonkim$ bundle exec rspec spec/requests/forums_spec.rb
................F

Failures:

  1) Forums new page should not let nonadmin users in 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `forums' for nil:NilClass
     # ./app/controllers/users_controller.rb:8:in `show'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:22:in `sign_in'
     # ./spec/requests/forums_spec.rb:60:in `block (4 levels) in <top (required)>'

Finished in 0.74504 seconds
17 examples, 1 failure

Failed examples:

rspec ./spec/requests/forums_spec.rb:65 # Forums new page should not let nonadmin users in 

3 个答案:

答案 0 :(得分:4)

我不确定,但这可能是范围问题。您在一个可能导致问题的示例之外调用user。尝试类似:

before do
  sign_in user
  visit new_forum_path
end
after { user.delete )

describe的开头,就像你之前的例子一样。

答案 1 :(得分:2)

let块正在创建一个在示例(实例)级别可用的方法,并且您直接在一个描述块中引用,该块位于示例组(类)级别。您只能在其他let中引用let,before,after,it和指定块。这是一个例子:

describe 'should not let nonadmin users in' do
  let(:user) { FactoryGirl.create(:user) }
  before do
    sign_in user
    visit new_forum_path
  end

  it { should have_selector('.alert.alert-notice', text:'You do not have permission to do that.') }

  after { user.delete }
end

看起来你在这里使用RSpec DSL并没有得到很多好处,但它似乎让事情变得更难,所以我建议平整事物:

it 'should not let nonadmin users in' do
  user = FactoryGirl.create(:user)
  sign_in user
  visit new_forum_path
  page.should have_selector('.alert.alert-notice', text:'You do not have permission to do that.')
end

after { user.delete }

如果该“用户”存在于本地数据库中,我还建议使用database_cleaner或类似的东西,而不是在规范末尾手动删除用户。

答案 2 :(得分:0)

您在规范的开头缺少声明:

it "should have not permitted selector" do
  sign_in user
  visit new_forum_path

  it { should have_selector('.alert.alert-notice', text:'You do not have permission to do that.') }
  user.delete
end