2013-02-07 50 views
-1

我收到此錯誤有關:「在微柱控制器提交給破壞行動認證授權」哈特爾教程第10章工廠女孩的錯誤 - 工廠未註冊:微柱

投機/ requests.authentication_pages_spec.rb: 119

我所看到的唯一解決方案涉及重新啓動rails服務器和/或spork(我沒有使用)已重新啓動服務器。

的git://github.com/princeofburma/sample_app.git

規格/ requests.authentication_pages_spec.rb

require 'spec_helper' 

describe "Authentication" do 

    subject { page } 

    describe "signin page" do 
    before { visit signin_path } 

    it { should have_selector('h1', text: 'Sign in') } 
    it { should have_selector('title', text: 'Sign in') } 
    end 

    describe "signin" do 
    before { visit signin_path } 

    describe "with invalid information" do 
     before { click_button "Sign in" } 

     it { should have_selector('title', text: 'Sign in') } 
     it { should have_error_message } 

     describe "after visiting another page" do 
     before { click_link "Home" } 
     it { should_not have_error_message } 
     end 
    end 

    describe "with valid information" do 
     let(:user) { FactoryGirl.create(:user) } 
     before { sign_in user } 

     it { should have_selector('title', text: user.name) } 
     it { should have_link('Profile', href: user_path(user)) } 
     it { should have_link('Sign out', href: signout_path) } 
     it { should have_link('Settings', href: edit_user_path(user)) } 
     it { should have_link('Users', href: users_path) } 
     it { should_not have_link('Sign in', href: signin_path) } 

     describe "followed by signout" do 
     before { click_link "Sign out" } 
     it { should have_link('Sign in') } 
     end 
    end 
    end 

    describe "authorization" do 

    describe "for non-signed-in users" do 
     let(:user) { FactoryGirl.create(:user) } 

     describe "when attempting to visit a protected page" do 
     before do 
      visit edit_user_path(user) 
      fill_in "Email", with: user.email 
      fill_in "Password", with: user.password 
      click_button "Sign in" 
     end 

     describe "after signing in" do 
      it "should render the desired protected page" do 
      page.should have_selector('title', text: 'Edit user') 
      end 

      describe "when signing in again" do 
      before do 
       click_link "Sign out" 
       click_link "Sign in" 
       fill_in "Email", with: user.email 
       fill_in "Password", with: user.password 
       click_button "Sign in" 
      end 

      it "should render the default (profile) page" do 
       page.should have_selector('title', text: user.name) 
      end 
      end 
     end 
     end 

     describe "in the Users controller" do 

     describe "visiting the edit page" do 
      before { visit edit_user_path(user) } 
      it { should have_selector('title', text: 'Sign in') } 
      it { should have_selector('div.alert.alert-notice') } 
     end 

     describe "submitting to the update action" do 
      before { put user_path(user) } 
      specify { response.should redirect_to(signin_path) } 
     end 

     describe "visiting the user index" do 
      before { visit users_path } 
      it { should have_selector('title', text: 'Sign in') } 
     end 

     describe "visiting the following page" do 
      before { visit following_user_path(user) } 
      it { should have_selector('title', text: 'Sign in') } 
     end 

     describe "visiting the followers page" do 
      before { visit followers_user_path(user) } 
      it { should have_selector('title', text: 'Sign in') } 
     end 
     end 
    end 

    describe "in the Microposts controller" do 

     describe "submitting to the create action" do 
     before { post microposts_path } 
     specify { response.should redirect_to(signin_path)} 
     end 

     describe "submitting to the destroy action" do 
     before { delete micropost_path(FactoryGirl.create(:micropost)) } 
     specify { response.should redirect_to(signin_path) } 
     end 
    end 


    describe "as wrong user" do 
     let(:user) { FactoryGirl.create(:user) } 
     let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") } 
     before { sign_in user } 

     describe "visiting Users#edit page" do 
     before { visit edit_user_path(wrong_user) } 
     it { should_not have_selector('title', text: 'Edit user') } 
     end 

     describe "submitting a PUT request to the Users#update action" do 
     before { put user_path(wrong_user) } 
     specify { response.should redirect_to(root_path) } 
     end 
    end 

    describe "as non-admin user" do 
     let(:user) { FactoryGirl.create(:user) } 
     let(:non_admin) { FactoryGirl.create(:user) } 

     before { sign_in non_admin } 

     describe "submitting a DELETE request to the Users#destroy action" do 
     before { delete user_path(user) } 
     specify { response.should redirect_to(root_path) } 
     end 
    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" 

    factory :admin do 
     admin true 
    end 
    end 

    factory :micropost do 
    content "Lorem ipsum" 
    user 
    end 
end 

這裏是spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 

# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

RSpec.configure do |config| 
    # ## Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    # Run specs in random order to surface order dependencies. If you find an 
    # order dependency and want to debug it, you can fix the order by providing 
    # the seed, which is printed after each run. 
    #  --seed 1234 
    config.order = "random" 
end 
從測試控制檯,並得到:10

跑FactoryGirl.create(微柱):

irb(main):002:0> FactoryGirl.create(:micropost) 
ArgumentError: Factory not registered: micropost 
     from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4 
.1.0/lib/factory_girl/registry.rb:24:in `find' 
     from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4 
.1.0/lib/factory_girl/decorator.rb:10:in `method_missing' 
     from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4 
.1.0/lib/factory_girl.rb:71:in `factory_by_name' 
     from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4 
.1.0/lib/factory_girl/factory_runner.rb:12:in `run' 
     from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4 
.1.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define 
_singular_strategy_method' 
     from (irb):2 
     from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.9 
/lib/rails/commands/console.rb:47:in `start' 
     from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.9 
/lib/rails/commands/console.rb:8:in `start' 
     from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.9 
     from script/rails:6:in `require 
from script/rails:6:in `<main>' 
+0

Bueller?任何人? – user2048191

+0

很棒,你發佈了你的Github repo供大家克隆,但請確保它包含你正在使用的代碼的最新版本,並且如果可能的話,沒有其他錯誤沒有在你的題。目前,當您[忘記添加一些遷移代碼](https://github.com/princeofburma/sample_app/blob/master/db/migrate/20121228170322_add_remember_token_to_users.rb)導致許多測試失敗。 –

+0

@ Paul ...好點...讓它通過。它現在在那裏。謝謝 – user2048191

回答

0

我覺得你的問題是在你的spec_helper.rb 來看看它是如何管理的工廠定義 https://gist.github.com/scottvrosenthal/1724370 我希望它幫助。

+0

@ Leftis ....我剛剛添加了上面的spec_helper.rb ...不知道如何將它與您鏈接到的代碼相關聯。 – user2048191

+0

@ user2048191爲您提供拉請求,以便factory_girl正常工作。 – Leftis

+0

@Leftis ...這是我現在得到的: – user2048191

0

我下載了你的項目,我沒有得到那個錯誤。我在應用程序中看到的唯一問題是您的User模型中沒有remember_token方法。

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation, :remember_token 
    ... 
    private 

    def create_remember_token 
     # NoMethodError: undefined method `remember_token=' for User 
     self.remember_token = SecureRandom.urlsafe_base64 
    end 
end 
+0

謝謝你......上面的代碼在我的app/models/user.rb – user2048191

+0

@ user2048191是的,但是我告訴你的是你在這個模型中有錯誤的代碼。看看我寫的'create_remember_token'裏面的註釋。 – Pigueiras

+0

這裏是app/models/user.rb的代碼 – user2048191