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

factory.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 
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 に答える