gpt4 book ai didi

ruby-on-rails - 带有 url 参数的 Rspec View 测试

转载 作者:行者123 更新时间:2023-11-28 20:54:28 25 4
gpt4 key购买 nike

我有一个页面 dashboard.html.erb 可以从几个不同的 Controller /操作重定向到。当它收到重定向时,它还会收到几个 url 参数。

Controller 代码:

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 进行的 Controller 测试将不起作用。因此,我曾考虑构建我的测试,以便 Controller 规范测试是否传递了正确的参数,而 View 规范测试是否存在某些参数、是否存在正确的 css 类或其他任何内容。但在我看来,我在模拟参数传递时遇到了麻烦。

这似乎是我发现的关于此的唯一其他问题:RSpec View testing: How to modify params?

但是没有一个解决方案对我有用;我不确定在这种情况下我需要一个 helper ……这似乎有点做作……

我已经尝试过(来自上面链接的 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>

他们都失败了:

最佳答案

Because these are redirects, controller testing with render_views will not work.

不,你只是做错了。

如果 StaticpagesController#dashboard 接受 url 参数,您应该测试它如何响应 StaticpagesController Controller 规范中的所述参数。

RSpec.describe StaticpagesController, type: :controller do 
describe 'GET #dashboard' do
render_views
it "does something with the x parameter" do
get :dashboard, x: 'hello!'
expect(response.body).to match 'hello!'
end
end
end

同样,您应该测试 PlansController#some_action 是否在响应中包含正确的参数。

我认为您的 View 规范问题告诉您您的 View 中有代码味道。在 MVC 中,您的 Controller 负责获取输入并将其传递给 View 和模型。将您的 View 与直接处理参数隔离开来意味着处理它的参数不会在您的整个应用程序中重复。

使用locals在您的 Controller 中将变量传递给您的 View ,并避免在您的 View 中使用 params

它有助于更​​好的设计和更容易的测试:

describe "rendering locals in a partial" do
it "displays the widget" do
widget = stub_model(Widget, :name => "slicer")
render :partial => "widgets/widget.html.erb", :locals => {:widget => widget}
rendered.should contain("slicer")
end
end

关于ruby-on-rails - 带有 url 参数的 Rspec View 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30992288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com