工厂女孩和蒙古人的问题

时间:2019-04-11 07:03:44

标签: ruby ruby-on-rails-4 rspec

我有这个要上课:

class User
     ...
has_and_belongs_to_many :posts
     ...
end

class Post
     ...
has_and_belongs_to_many :users

validates :user_ids, presence: true
     ...
end

那我也有这个工厂:

FactoryGirl.define do

   factory :user do
   end

end


FactoryGirl.define do

   factory :post do
   end

end

在我进行验证之前,验证:user_ids,presence:true,它运行良好,但是当我添加该验证时,它以表格形式表示错误,因此很好。但是rspec无效:

    Mongoid::Errors::Validations:

      message:
        Validation of User failed.
      summary:
        The following errors were found: Customer ids can't be blank
      resolution:
        Try persisting the document with valid data or remove the validations.

所以我尝试了这个:

issue on github but for activerecord

blog post

并尝试了以下方法:

factory :post do
  before_create do |post|
    FactoryGirl.build(:user, post: post)
  end
end

before(:create) do |post|
  post.users << FactoryGirl.build(:user, post: post)
end

 before(:create) do |post|
      post.users << FactoryGirl.create(:user, post: post)
    end

users { create_list(:user, 2) }

users {[create(:user,posts:[])]}

但是我得到:

 bundle exec rspec spec --colour --format documentation                                                                                                                                                
/Users/toni/.rvm/gems/ruby@my-project/bin/ruby_executable_hooks:15: stack level too deep (SystemStackError)

0 个答案:

没有答案