2014-09-26 61 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