Ruby on rails 当一个模型与另一个类的模型交互时,组织Rspec测试的最佳方法是什么?
为了便于论证,假设我有一个Ruby on rails 当一个模型与另一个类的模型交互时,组织Rspec测试的最佳方法是什么?,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,为了便于论证,假设我有一个事件模型,该模型需要在首次创建事件时创建一个通知。例如,在Rails中使用创建后的回调 我应该将回调测试放在哪个规范中?例如型号/事件规范rb型号/通知规格rb?作为集成测试的其他地方 以下是我目前的思考过程: 我的第一反应是将其放在事件规范rb中: describe Event do ... describe 'callbacks' do it 'should create a Notification when first saved' do
事件
模型,该模型需要在首次创建事件时创建一个通知
。例如,在Rails中使用创建后的回调
我应该将回调测试放在哪个规范中?例如型号/事件规范rb
<代码>型号/通知规格rb
?作为集成测试的其他地方
以下是我目前的思考过程:
我的第一反应是将其放在事件规范rb
中:
describe Event do
...
describe 'callbacks' do
it 'should create a Notification when first saved' do
# assertions here
end
end
end
describe Notification do
...
describe 'callbacks' do
it 'should be created when new an Event is first saved' do
# assertions here
end
end
end
然而,这感觉就像我打破了关注点的分离。例如,我正在测试在事件
规范中是否创建了通知
。然后我认为将这些测试放在通知规范rb
中可能更合适:
describe Event do
...
describe 'callbacks' do
it 'should create a Notification when first saved' do
# assertions here
end
end
end
describe Notification do
...
describe 'callbacks' do
it 'should be created when new an Event is first saved' do
# assertions here
end
end
end
但这感觉也不对,因为我们现在正在测试通知
规范中事件
类的回调代码
有什么想法吗?这属于事件
规范。事件
负责创建通知
。此外,回调代码位于事件
源文件中,因此查找回调测试的人通常希望在事件
规范中找到它
更重要的问题是:如何测试它?“”将完全隔离通知
模型:
it "should create a Notification" do
Notification.should_receive(:create)
event.save!
end
这确保了回调创建通知,但不运行create方法。这也是我所倾向的。我喜欢使用应该接收!