未定义的方法“each”在factory_girl / rspec2场景中

发布于 2024-09-24 09:40:56 字数 714 浏览 3 评论 0原文

我正在尝试创建与投票相关的帖子。这样 Post.votes 将生成与其关联的投票。

Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.association :votes, :factory => :vote
end

我的 rspec2 相对简单:

describe "vote scores" do
  it "should show me the total vote score" do
    @post = Factory(:voted_post)
    @post.vote_score.should == 1
  end
end

那么为什么它会返回此错误:

Failures:
   1) Post vote scores should show me the total vote score
     Failure/Error: @post = Factory(:voted_post)
     undefined method `each' for #<Vote:0x105819948>

ruby​​ 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

Rails 3.0.0

I'm trying to Factory a Post associated with a Vote. So that Post.votes would generate the Vote's that are associated with it.

Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.association :votes, :factory => :vote
end

And my rspec2 is relatively straightforward :

describe "vote scores" do
  it "should show me the total vote score" do
    @post = Factory(:voted_post)
    @post.vote_score.should == 1
  end
end

So why would it return this error :

Failures:
   1) Post vote scores should show me the total vote score
     Failure/Error: @post = Factory(:voted_post)
     undefined method `each' for #<Vote:0x105819948>

ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

Rails 3.0.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-10-01 09:40:56
Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.association :votes, :factory => :vote
end

与尝试去相同

some_voted_post.votes = Factory(:vote)

基本上您正在尝试将单个投票分配为数组。

编辑

您可以拥有一个包含单票的数组,但不能只拥有单票。

之间的区别是:

some_voted_post.votes = Factory(:vote)

some_voted_post.votes = [Factory(:vote)]

前者不是数组,因此不起作用,后者是数组。

Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.association :votes, :factory => :vote
end

Is the same as trying to go

some_voted_post.votes = Factory(:vote)

Basically you're attempting to assign a single vote as an array.

EDIT

You can have an array containing a single vote, but you can't just have a single vote.

It's the difference between:

some_voted_post.votes = Factory(:vote)

and

some_voted_post.votes = [Factory(:vote)]

The former is not an array, and therefore does not work, the latter is an array.

碍人泪离人颜 2024-10-01 09:40:56

如果你想分配 has_many 关联,它需要数组而不是单个值,你应该使用长格式:

Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.votes { |vote| [vote.association(:vote)] }
end

并用 [] 封装关联的创建以确保返回数组

If you want to assign has_many association which expects array and not a single value, you should use the long form:

Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.votes { |vote| [vote.association(:vote)] }
end

And encapsulate the creation of the association with [] to ensure that array would be returned

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文