【发布时间】:2019-09-17 17:49:46
【问题描述】:
我正在尝试编写一个测试来查看我的 Hospital 模型是否正在接收自定义方法 look_for_hospitals。
这是测试:
Rspec.describe HospitalsController, type: :controller do
describe '#search' do
it 'should call the model method to do the search' do
# model should implement method look_for_hospitals
expect(Hospital).to receive(:look_for_hospitals).with('keyword')
# form in search page must be named 'keywords'
get :search, params: {:keywords => 'keyword'}
expect(assigns(:hospitals))
end
end
这是我的模型:
class Hospital<ApplicationRecord
def self.look_for_hospitals term
end
end
这里是 HospitalsController 中的方法搜索:
def search
keyword = params[:keywords]
@hospitals = Hospital.look_for_hospitals(keyword)
end
当我运行测试时,这是我遇到的错误:
1) HospitalsController#search should call the model method to do the search
Failure/Error: expect(Hospital).to receive(:look_for_hospitals).with('keyword')
(Hospital(id: integer, cnes: string, number: integer, address: text, latitude: string, longitude: string, name: string, phones: text, nature: string, specialties: text, rpa: string, microregion: string, created_at: datetime, updated_at: datetime) (class)).look_for_hospitals("keyword")
expected: 1 time with arguments: ("keyword")
received: 0 times
我知道几乎没有任何实现,但我正在尝试先编写测试然后编写方法的 tdd 方法。 对不起,如果我的英语有点奇怪,不是以英语为母语的人。
谢谢!
【问题讨论】:
-
你试过调试吗?你发现了什么?该问题可能是由各种原因引起的,但我很确定这是您在上面的帖子中没有显示的问题。 (例如,控制器上可能有一个
before_action,这意味着该行代码永远不会被执行?) -
就是这样。有人在项目中添加了设计,我完全错过了
before_action行。非常感谢! -
棘手棘手..
-
如果您不想关闭问题,请随时留下您自己的答案,您可以在几天后接受。这将有助于推动它,因为您已经知道它的答案,并且回答者不必通读它就知道它已经解决了。
-
你明白了。我是新来的,如果我还不知道协议,很抱歉。
标签: ruby-on-rails ruby rspec tdd