【发布时间】:2015-06-23 01:08:54
【问题描述】:
我有一个页面dashboard.html.erb,可以从几个不同的控制器/操作重定向到该页面。当它接收到重定向时,它也接收到几个 url 参数。
控制器代码:
class PlansController
def some_action
redirect_to dashboard_path(show: "default", cool_array: ["test", "test"])
end
end
class StaticpagesController
def dashboard
end
end
查看代码:
<% if cool_array %>
<% cool_array.each do |a| %>
<li><%= a %></li>
<% end %>
<% end %>
<script>
var show = getParamFromURL("show")
// some sort of function that returns the value of the show param, in this case, "default"
if (show == "default") {
$("#default").addClass("show");
} else {
$("#other").addClass("hidden");
}
</script>
因为这些是重定向,所以使用render_views 进行控制器测试将不起作用。因此,我曾考虑构建我的测试,以便控制器规范测试是否传递了正确的参数,并且视图规范测试是否存在某些参数,是否存在正确的 css 类,或其他。但在我看来,我在模拟参数传递时遇到了麻烦。
这似乎是我发现的唯一其他问题:RSpec View testing: How to modify params?
但是没有一个解决方案对我有用;我不确定在这种情况下我是否需要一个帮手……这似乎有点做作……
我已经尝试过(来自上面链接的 SO 问题):
before do
controller.request.path_parameters[:cool_array] = ["test","test"]
end
# => ActionView::Template::Error:
# undefined local variable or method `cool_array' for #<#<Class:0x007fe2d9815100>:0x007fe2d90a62e8>
# thrown by line in view file with <% if cool_array %>
before do
view.stub(:params).and_return({cool_array: ["test", "test"]})
end
# => ActionView::Template::Error:
# undefined local variable or method `cool_array' for #<#<Class:0x007fe2d9815100>:0x007fe2d90a62e8>
# thrown by line in view file with <% if cool_array %>
helper.params = {:cool_array => ["test", "test"]]}
# honestly not even sure this is right, because it can only go into a describe block (not a before, not an it) and then it throws:
# NoMethodError:
# undefined method `params=' for #<#<Class:0x007fd7cc8f4930>:0x007fd7cc8a5060>
他们都失败了:
【问题讨论】:
标签: ruby-on-rails unit-testing testing rspec rspec-rails