2011-08-24 53 views
0

使用RSpec的和設計,我得到一個映射錯誤的有效映射。Rspec的並設計:找不到#<用戶

1) BusinessesController GET index sets @new_vacation 
    Failure/Error: sign_in @user 
    RuntimeError: 
     Could not find a valid mapping for #<User id: 8009, email: "[email protected]", encrypted_password: "$2a$10$joN9Vy6spNAe4uh7W9fcOOdoeSJ5dr/nBwpmlqcv6EYe...", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2011-04-01 17:05:03", updated_at: "2011-04-01 17:05:03", business_id: 7214, confirmation_token: nil, confirmed_at: "2011-08-24 21:23:51", confirmation_sent_at: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, admin: true, password_salt: nil> 
    # ./spec/controllers/businesses_controller_spec.rb:10:in `block (2 levels) in <top (required)>' 

看起來答案可能在這裏RSpec tests with devise: could not find valid mapping但我無法得到它。

任何想法,我真的難倒!謝謝。


的routes.rb

constraints(SubdomainRoute) do 

    # SIGN IN 
    devise_for :accounts, :controllers => { 
                 :sessions => "users/accounts/sessions", 
                 :confirmations => "users/accounts/confirmations" }, 
                 :class_name => 'Admin' do 
     get "accounts/sign_in", :to => "users/accounts/sessions#new" 

     get "accounts/sign_up", :to => "users/devise/registrations#new" 
     get "accounts/signedup", :to => "users/devise/confirmations#signedup", :as => "signedup_registration" 
    end 
end 

admin.rb

class Admin < User 
    validates_associated :business 
end 

user.rb

class User < ActiveRecord::Base 
    belongs_to :business 
    accepts_nested_attributes_for :business 

    # TODO get lockable working -- failed_attempts is incrementing in db 
    devise :database_authenticatable, :registerable, :confirmable, :lockable, 
     :recoverable, :rememberable, :trackable, :validatable 
end 

factories.rb

FactoryGirl.define do 

    Factory.define :business do |b| 
    end 

    Factory.define :business_main, :parent => :business do |b| 
    b.business_name "Ann's Salon" 
    b.address_line1 "123 Main Street" 
    b.address_line2 "" 
    b.city "Portland" 
    b.state "Oregon" 
    b.zip "97211" 
    end 

    factory :admin do 
    confirmed_at Time.now 

    factory :admin_one do 
     association :business, :factory => :business_one 
     email 
     password 'please' 
     admin true 
    end 
    end 
end 

business_controller_spec.rb

require 'spec_helper' 

describe BusinessesController do 

    before (:each) do 
    @business = Factory(:business_one) 
    @user  = User.find_by_business_id(@business.id) 
    sign_in @user 
    end 

    describe "GET index" do 

    let(:describe_action) { get :index } 

    it "sets @new_vacation" do 
     BusinessVacation.should_receive(:new) 
     get :index 
    end 
    end 

end 

回答

-1

我的問題是無關的代碼。我最近添加了一個before-filter來總是重定向到www。

redirect_to request.protocol + "www." + request.host_with_port + request.fullpath if !/^www/.match(request.host) 

我仍然不完全確定爲什麼這導致了我的問題,但它確實如此。

0

你可能要檢查你的代碼多devise_for :users聲明在不同的地方。這在我的情況下是這種例外的原因,因爲它肯定會把Devise弄糊塗。

6

我在routes文件有這個錯誤,因爲我有,我想允許用戶自己的密碼重置從一個API端點,但我希望用戶能夠真正做到在Web視圖中的密碼更改,這是不/api

下的命名空間這是我的固定路線,使其工作:

CoolApp::Application.routes.draw do 
    namespace :api, defaults: { format: :json } do 
    devise_scope :users do 
     post 'users/passwords', to: 'passwords#create' 
    end 

    resources :users, only: [:create, :update] 
    end 

    devise_for :users 

    root to: 'high_voltage/pages#show', id: 'home' 
end 
+2

對我來說剛剛添加的罰款「devise_for:用戶和作品。謝謝@尼爾。 – monteirobrena

+1

我想給這100萬upvotes。根本找不到遺漏的東西! – TimeEmit

1

首先確保你已經在你的路由都被稱爲「devise_for」,並在模型中「色器件」。我建議您閱讀自述文件和wiki中的文檔以獲取更多信息。

如果是的話試試這個:

將這個代碼放到你的application.rb中

ActionDispatch::Callbacks.after do  
    # Reload the factories 
    return unless (Rails.env.development? || Rails.env.test?) 

    unless FactoryGirl.factories.blank? # first init will load factories, this should only run on subsequent reloads 
    FactoryGirl.factories.clear 
    FactoryGirl.find_definitions 
    end 

end  

這是因爲設計使用類和路線之間的映射,所以當工廠建成對象來通過制定在控制檯重新加載或類重新定義之後,它將失敗。這在開發和測試環境中通常是這種情況。

我在這裏找到&我 - http://blog.thefrontiergroup.com.au/2011/03/reloading-factory-girl-factories-in-the-rails-3-console/

+0

在rails控制檯中做'reload!'導致了設計映射的相同問題。完全控制檯重新啓動修復它 – Raccoon