[英]How to test this Rspec Ajax request
I'm trying to test the scenarios method in my show action 我正在尝试在show action中测试场景方法
def scenarios
@scenarios ||= fund.present? ? fund.scenarios : []
end
def fund
@fund ||= funds.find_by(id: fund_id)
end
def fund_id
return nil unless params.fetch(:report, false)
params.fetch(:report).fetch(:fund_id, 0)
end
I feel like i need to get the ajax request and grab the params of fund_id out of it but I am unsure how to do that or if that is what I am suppose to do 我觉得我需要获取ajax请求并从中获取fund_id的参数,但是我不确定如何执行此操作,或者不确定是否应该执行此操作
this is my my ajax request in my js file 这是我在我的js文件中的ajax请求
$('#geodistributions-filter-form-container')
.on('change', 'select#report_fund_id', function() {
$.get('/reports/geodistribution.js',
$(this).parents('form').serialize());
});
I want to test two contexts 我想测试两个上下文
context 'scenario when a fund is not present' 上下文“不存在基金的情况”
context 'scenario when a fund is present' 上下文“存在资金时的场景”
I am unsure how to start off testing this if someone can point me in the right direction that would be great. 我不确定如果有人可以指出正确的方向,那将是很好的尝试。
Well, if I understood you correctly, you want to test if your ajax calls. 好吧,如果我对您的理解正确,那么您想测试一下ajax是否调用。 You can use acceptance tests frameworks like Capybara or simply test your call to /reports/geodistribution.js
URL just like any other controller call. 您可以使用验收测试框架,比如水豚或简单地测试您的来电/reports/geodistribution.js
URL就像任何其他控制器的呼叫。 Setup the required environment (with Fund
, without Fund
) and check it behaves correctly. 设置所需的环境(使用Fund
,不使用Fund
)并检查其行为是否正确。
I would also refactor your code a bit: 我还将重构您的代码:
def scenarios
@scenarios ||= fund.try(:scenarios) : []
end
def fund
# if there is no fund found, let it throw an exception
@fund ||= Funds.find(params[:report][:fund_id])
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.