test 'index for local seller (same site)' do seller = FactoryGirl.create :seller,business_site: @open_or.business_site get :index,nil,{user_id: seller.to_param } assert_select "table#order_requests tr##{@controller.view_context.dom_id @open_or}" end test 'index for local seller (different site)' do seller = FactoryGirl.create :seller_local get :index,{user_id: seller.to_param } assert_select "table#order_requests tr##{@controller.view_context.dom_id @open_or}",false end
我希望在一个测试下合并,但如果我这样做,第二个断言将错误地失败(预期正好0个元素匹配“table#order_requests tr#order_request_1000244799”,找到1.).我真的不明白为什么?第二次’get’调用可能无法正常重置某些内容.我想方设法“重置”请求但没有成功.
相关:making two requests to the same controller in rails integrations specs
解决方法
class ThingyController < ApplicationController def one @thingy = Thingy.first render :nothing => true end def two render :nothing => true end end
行动’一’设置@ thingy,而动作’一’设置不行.但是,此功能测试失败:
test "thingy should not exist in second request" do get :one assert_not_nil assigns(:thingy),"thingy should exist in get_one" get :two assert_nil assigns(:thingy),"thingy should be absent in get_two" end
据推测,这是因为第一个请求中的@thingy在测试方法的持续时间内仍然作为控制器中的实例变量.
虽然你可以在一次测试中进行多次get / put / etc调用,但我认为它们不是为每个测试方法测试多个动作而设计的.按Rails API TestCase docs:
Functional tests allow you to test a single controller action per test
method. This should not be confused with integration tests (see
Actiondispatch::IntegrationTest),which are more like “stories” that
can involve multiple controllers and multiple actions (i.e. multiple
different HTTP requests).
如果您真的希望将这两个操作结合起来,那么集成测试可能是更好的方法.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。