0

我不断收到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

工厂.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

登录方法

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'

将答案更改为 avendt 后出现新错误

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 
4

3 回答 3

4

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

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

在 的开头describe,就像您之前对示例所做的那样。

于 2012-07-18T07:15:08.637 回答
2

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

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 或类似的东西,而不是在规范结束时手动删除用户。

于 2012-07-22T20:47:27.153 回答
0

您在规范开始时缺少 it 声明:

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
于 2012-07-18T07:24:08.270 回答