RSpec 验证在通过 Factory_girl 建立的关联上失败

发布于 2024-11-29 06:34:50 字数 1125 浏览 1 评论 0原文

我确信尝试一些简单的事情而忽略了显而易见的事情。 Factory_Girl 不应该自动创建关联吗?如果是这样,为什么下面的“GET index”规范会失败,因为 event_idnil

事件 has_many :posts

#post.rb
  ...
  belongs_to :event
  validates :event_id, :presence => true
  ...

#factories.rb
Factory.define :event do |event|
  event.name "The Long Event Title"
end

Factory.define :post do |post|
  post.title "This is a post"
  post.association :event
end


#posts_controller_spec.rb
before(:each) do
  @attr = Factory.attributes_for(:post)
end

describe "GET index" do
  it "assigns all posts as @posts" do
    post = @user.posts.create(@attr)    ### <-- event_id not assigned?
    # post = FactoryGirl.create(:post)  ### <-- this doesn't work either
    get :index
    assigns(:posts).should eq([post])
  end
end

编辑: 其他规范示例:

describe "POST create" do
    describe "with valid params" do
      it "creates a new Post" do
        expect {
          post :create, :post => @attr, :event => Factory.create(:event)  <- fail
        }.to change(Post, :count).by(1)
      end

Trying something simple and overlooking the obvious I'm sure. Shouldn't Factory_Girl be automatically creating the association? If so why does "GET index" spec below fail because event_id is nil?

Event has_many :posts

#post.rb
  ...
  belongs_to :event
  validates :event_id, :presence => true
  ...

#factories.rb
Factory.define :event do |event|
  event.name "The Long Event Title"
end

Factory.define :post do |post|
  post.title "This is a post"
  post.association :event
end


#posts_controller_spec.rb
before(:each) do
  @attr = Factory.attributes_for(:post)
end

describe "GET index" do
  it "assigns all posts as @posts" do
    post = @user.posts.create(@attr)    ### <-- event_id not assigned?
    # post = FactoryGirl.create(:post)  ### <-- this doesn't work either
    get :index
    assigns(:posts).should eq([post])
  end
end

Edit: Additional spec example:

describe "POST create" do
    describe "with valid params" do
      it "creates a new Post" do
        expect {
          post :create, :post => @attr, :event => Factory.create(:event)  <- fail
        }.to change(Post, :count).by(1)
      end

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

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

发布评论

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

评论(2

心房的律动 2024-12-06 06:34:50

来自 FactoryGirl wiki:https://github.com/thoughtbot/factory_girl/wiki/Usage

# Attribute hash (ignores associations)
user_attributes = Factory.attributes_for(:user)

所以你没有得到 event_id,这就是它失败的原因。

另外,您说您尝试了 post = FactoryGirl.create(:post) 但您应该执行 post = Factory.create(:post) 这将使其正常工作。

也许在您的 before() 块中,您应该只创建并保存帖子,除非您有一个测试要求尚未保存它。

From the FactoryGirl wiki: https://github.com/thoughtbot/factory_girl/wiki/Usage

# Attribute hash (ignores associations)
user_attributes = Factory.attributes_for(:user)

So you are not getting an event_id, and that's why it's failing.

Also, you said you tried post = FactoryGirl.create(:post) but you should do post = Factory.create(:post) and that will get it to work.

Perhaps in your before() block you should just create and save the post, unless you have a test that requires it to not be saved yet.

青柠芒果 2024-12-06 06:34:50

尝试更改

validates_presence_of :event_id

validates_presence_of :event

Try changing

validates_presence_of :event_id

to

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