gpt4 book ai didi

ruby - 使用 rspec 测试 after_create

转载 作者:数据小太阳 更新时间:2023-10-29 08:39:25 24 4
gpt4 key购买 nike

我的模型中有代码。

class Foo < ActiveRecord::Base
after_create :create_node_for_foo

def create_node_for_user
FooBar.create(id: self.id)
end
end

并且在 Foo 模型的 rspec 中有代码

describe Foo do

let (:foo) {FactoryGirl.create(:foo)}

subject { foo }
it { should respond_to(:email) }
it { should respond_to(:fullname) }

it "have mass assignable attributes" do
foo.should allow_mass_assignment_of :email
foo.should allow_mass_assignment_of :fullname
end

it "create node in graph database" do
foo1 = FactoryGirl.create(:foo)
FooBar.should_receive(:create).with(id: foo1.id)
end
end

但我的测试失败并显示消息

Failures:

1) Foo create node in graph database on
Failure/Error: FooBar.should_receive(:create).with(id: foo1.id)
(<FooBar (class)>).create({:id=>18})
expected: 1 time
received: 0 times

可能出了什么问题?

最佳答案

好的解决了问题

改变了这个

 it "create node in graph database" do
foo1 = FactoryGirl.create(:foo)
FooBar.should_receive(:create).with(id: foo1.id)
end

 it "create node in graph database" do
foo1 = FactoryGirl.build(:foo)
FooBar.should_receive(:create).with(id: foo1.id)
foo1.save
end

关于ruby - 使用 rspec 测试 after_create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com