2014-09-26 49 views
0

因此,遵循Hartl示例應用程序教程,並且在獲取一些有關關係規範的測試過程中遇到了一些問題。Rails Sample_App,測試錯誤

在我的規格文件我有

describe "follow methods" do 

    before(:each) do 
    @follower = User.create!(:name => "Example User", 
     :email => "[email protected]", 
     :password => "foobar", 
     :password_confirmation => "foobar" 
    ) 
    @followed = User.create!(:name => "Example User", 
     :email => "[email protected]", 
     :password => "foobar", 
     :password_confirmation => "foobar" 
    ) 
    @attr = { :followed_id => @followed.id } 
    @relationship = @follower.relationships.create!(@attr) 

    end 

我知道這是不一樣的書但我不使用FactoryGirl。

當使用這個塊進行測試時,我得到一個錯誤,我的@attr讀取錯誤;

Failure/Error: @attr = { :followed_id => @followed.id } 
NoMethodError: 
    undefined method `id' for #<Hash:0x00000108d0b2a0> 

但是,當我在控制檯中運行此塊時,它工作得很好。該id是根據@attr設置的,我可以在@followed上調用.id。

任何幫助將非常感激,一如既往!

+0

如果你使用'.create(會發生什麼)'而不是'.create(! )' – sircapsalot 2014-09-26 17:37:41

+0

發生同樣的錯誤! – user2085143 2014-09-26 17:39:19

+0

嘗試使用'@ followlow ['id']'而不是'@ followed.id' – sircapsalot 2014-09-26 17:47:37

回答

0

根據你的錯誤,方法id不存在一個哈希。

要訪問,您可以使用此:

@attr = { :followed_id => @followed['id'] } 

的問題是你試圖做@followed.id