这些控制器规格足够好吗?
我是使用 RSpec 测试控制器的新手。在我的控制器中,我有这些方法:
def edit
@widget = Widget.find(params[:id])
if @widget.present?
@all_widgets = Widget.where(:id != @widget.id).select("id, title")
wfs @widget.id
else
redirect_to widgets_url
end
end
并且
def wfs widget_id
@all_features,
@existing_features,
@draft_features,
@imported_features = WidgetFeature.find_by_widget_id_group_by_status(widget_id)
end
我已经在相应的模型中测试了方法 WidgetFeature.find_by_widget_id_group_by_status(widget_id)
。
现在,我不知道如何测试 edit
方法以及测试什么。我了解存根、模拟和双打的一切。我正在遵循Rails Test Prescriptions和RSpec 书。 因此,我熟悉术语和基础知识,但是我很困惑何时模拟或为数据创建工厂。
另外,如何测试 edit 方法实际上调用 wfs 方法?
更新:
这些是我编写的一些规范,覆盖率达到 100%(由 SimpleCov 测试)。这是测试上述方法的最佳方法吗?
context " When #edit is called, it" do
it "assigns @all_widgets & other variables if widget for given widget_id is present" do
widget = Factory.create(:widget)
get :edit, :id => widget.id
assigns.each {|a| a.should be_kind_of(Array)}
response.should be_success
response.should render_template("edit")
end
it "redirects_to widgets_url if widget for given widget_id is not present" do
widget = Widget.stub!(:find).with(12)
get :edit, :id => 12
response.should redirect_to(widgets_url)
response.should be_redirect
end
end
欢迎就如何改进上述规格提出意见
I am new to testing controllers with RSpec. In my controller, I have these methods:
def edit
@widget = Widget.find(params[:id])
if @widget.present?
@all_widgets = Widget.where(:id != @widget.id).select("id, title")
wfs @widget.id
else
redirect_to widgets_url
end
end
and
def wfs widget_id
@all_features,
@existing_features,
@draft_features,
@imported_features = WidgetFeature.find_by_widget_id_group_by_status(widget_id)
end
I have tested the method WidgetFeature.find_by_widget_id_group_by_status(widget_id)
in the corresponding model.
Now, I don't know how to test the edit
method and what to test. I know all about stubs, mocks and doubles. I am following the Rails Test Prescriptions and the RSpec book.
So, I am familiar with the terminology and the basics, however I am confused when to mock or make a factory for the data.
Also, how do I test that the edit method actually calls the wfs method?
UPDATE:
These are some specs I have written, that give 100% coverage (tested by SimpleCov). Is this the best way to test the above methods.
context " When #edit is called, it" do
it "assigns @all_widgets & other variables if widget for given widget_id is present" do
widget = Factory.create(:widget)
get :edit, :id => widget.id
assigns.each {|a| a.should be_kind_of(Array)}
response.should be_success
response.should render_template("edit")
end
it "redirects_to widgets_url if widget for given widget_id is not present" do
widget = Widget.stub!(:find).with(12)
get :edit, :id => 12
response.should redirect_to(widgets_url)
response.should be_redirect
end
end
Comments are welcome on how to improve the above specs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该模拟 WidgetFeature。这样测试会更快,因为它不会影响数据库。要检查edit方法是否正在调用wfs方法,可以使用should_receive。
You should mock WidgetFeature. In that way tests will be faster as it does not hit DB. And to check whether edit method is calling wfs method, you can use should_receive.