0

我正在关注 Rails 教程,并被告知在我的静态页面控制器中输入:

def home
    if signed_in?
        @micropost  = current_user.microposts.build
        @feed_items = current_user.feed.paginate(page: params[:page])
    end
end

以下是 RSpec 测试的结果:

Failures:

  1) Static Pages Home Page for signed in users should render user's feed
     ←[31mFailure/Error:←[0m ←[31mvisit root_path←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5ae7480>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/static_pages_spec.rb:29:in `block (4 levels) in <top (required)>'←[0m

  2) User
     ←[31mFailure/Error:←[0m ←[31mit { should respond_to(:feed) }←[0m
       ←[31mexpected #<User id: nil, name: "Tim Green", email: "timgreen1@outlook.com", created_at: nil, updated_at: nil, password_digest: "$2a$04$5zT
aqLxQ5E6zT5DU.gGmaeS2qV9ypLOBdwJiXxu8WkjN...", remember_token: nil, admin: false> to respond to :feed←[0m
←[36m     # ./spec/models/user_spec.rb:32:in `block (2 levels) in <top (required)>'←[0m

  3) User micropost associations status feed
     ←[31mFailure/Error:←[0m ←[31mits(:feed) { should_not include(unfollowed_post) }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5bb1c80>←[0m
←[36m     # ./spec/models/user_spec.rb:173:in `block (4 levels) in <top (required)>'←[0m

  4) User micropost associations status feed
     ←[31mFailure/Error:←[0m ←[31mits(:feed) { should include(older_micropost) }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5bdfdd8>←[0m
←[36m     # ./spec/models/user_spec.rb:172:in `block (4 levels) in <top (required)>'←[0m

  5) User micropost associations status feed
     ←[31mFailure/Error:←[0m ←[31mits(:feed) { should include(newer_micropost) }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5c6f718>←[0m
←[36m     # ./spec/models/user_spec.rb:171:in `block (4 levels) in <top (required)>'←[0m

  6) Authentication authorization as wrong user visiting edit page for different user
     ←[31mFailure/Error:←[0m ←[31mbefore { visit edit_user_path(wrong_user) }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x6149358>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:110:in `block (5 levels) in <top (required)>'←[0m

  7) Micropost pages micropost creation with invalid information should not create a micropost
     ←[31mFailure/Error:←[0m ←[31mbefore { visit root_path }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x61a3df0>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'←[0m

  8) Micropost pages micropost creation with invalid information error messages
     ←[31mFailure/Error:←[0m ←[31mbefore { visit root_path }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x43aaba0>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'←[0m

  9) Micropost pages micropost creation with valid information should create a micropost
     ←[31mFailure/Error:←[0m ←[31mbefore { visit root_path }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `feed' for #<User:0x5a73f50>←[0m
←[36m     # ./app/controllers/static_pages_controller.rb:6:in `home'←[0m
←[36m     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'←[0m

Finished in 7.74 seconds
←[31m114 examples, 9 failures←[0m

基本上它为“feed”方法返回 NoMethodError,我理解错误,但我找不到在哪里定义这个方法以及如何去做,记住这将返回一个新闻提要,有什么帮助吗?

4

1 回答 1

1

这些错误会给你提示。这个:

undefined method `feed' for #<User:0x5ae7480>

表明您有一个 User 类的实例,并且代码正在尝试查找feed在该 User 实例上命名的实例方法。

还有一个失败的测试直接测试这个:

expected #<User id: nil, name: "Tim Green", email: "timgreen1@outlook.com", 
created_at: nil, updated_at: nil, password_digest: "$2a$04$5zTaqLxQ5E6zT5DU.gGmaeS2qV9ypLOBdwJiXxu8WkjN...", 
remember_token: nil, admin: false> to respond to :feed

这在 user_spec.rb 中,明确表示 User 实例应该响应名为 的消息(这是“方法调用”的另一种说法)feed

这意味着您需要转到您的 user.rb 并添加一个def feed并实现该方法以返回您希望它返回的内容。

于 2013-10-06T16:27:48.333 回答