2009-12-29 39 views
20

加載所以,我努力學習在Rails項目的背景下,RSpec的BDD測試框架。我遇到的問題是,在我的生活中,我不能讓我的設備在rspec描述中正確加載。Rails的燈具不使用RSpec

免責聲明:是的,有比燈具使用更好的東西。我想了解一件事情的時候,這裏(特別是RSpec的)之前,我去與像工廠女孩,摩卡,自動測試等。因此相關的工具發揮,我試圖讓死簡單,如果笨重,裝置工作。

總之,這裏的代碼:

/test/fixtures/users.yml -

# password: "secret" 
foo: 
    username: foo 
    email: [email protected] 
    password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f 
    password_salt: bef65e058905c379436d80d1a32e7374b139e7b0 

bar: 
    username: bar 
    email: [email protected] 
    password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f 
    password_salt: bef65e058905c379436d80d1a32e7374b139e7b0 

/spec/controllers/pages_controller_spec.rb -

require 'spec/spec_helper' 

describe PagesController do 
    integrate_views 
    fixtures :users 
    it "should render index template on index call when logged in" do 
    session[:user_id] = user(:foo).id 
    get 'index' 
    response.should render_template('index') 
    end 
end 

什麼我當我運行'耙規格'時得到的是:

NoMethodError in 'PagesController should render index template on index call when logged in' 
undefined method `user' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0x2405a7c> 
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/test_process.rb:511:in `method_missing' 
./spec/controllers/pages_controller_spec.rb:7: 

也就是說,它不會將'user(:foo)'識別爲有效的方法。

的燈具本身必須是確定的,因爲當我通過「耙分貝:燈具:負載」它們加載到開發數據庫,​​我可以驗證foo和酒吧都存在於數據庫中。

我覺得我缺少明顯的東西在這裏,但我整天都撕裂了我的頭髮都無濟於事。任何幫助,將不勝感激。

回答

32

如果定義爲燈具「用戶」,然後使用它們是通過具有相同名稱的方法方式:

describe PagesController do 
    integrate_views 
    fixtures :users 
    it "should render index template on index call when logged in" do 
    session[:user_id] = users(:foo).id 
    get 'index' 
    response.should render_template('index') 
    end 
end 

單數只類本身(用戶)有關。如果這只是一個字母的錯誤,希望你還剩下一些頭髮。

+1

該死。那麼,我確定這是一件愚蠢的事情,我確實是該死的。謝謝,這解決了它。 :) – Fishtoaster 2009-12-29 20:49:48

+0

@tadman你在線嗎? – 2014-12-06 18:35:34

7

如果你想在全球範圍內建立的燈具,你spec_helperrails_helper您可以添加內部:

RSpec.configure do |config| 
    config.global_fixtures = :all 
end