是否建议在 Rails 测试环境中禁用某些辅助方法?
我有一个应用程序帮助器,它确定应用程序帮助器顶部导航栏的 css 类(选定或未选择)和链接地址。
当我测试控制器时,我收到一大堆有关导航栏的错误(变量返回 nil)。 由于导航栏与每个单独的控制器无关,并且它出现在每个页面上,因此最简单的事情似乎就是这样。
在应用程序帮助器中:
def navbar_method(category)
c = Category.find_by_name(category)
children = c.children.map {|child| child.name}
if category == "All"
if params[:id] == category || params[:category] == category
"on"
else
"off"
end
else
if params[:id] == category || children.include?(params[:id]) || params[:category] == category || children.include?(params[:category])
"on"
else
"off"
end
end
这是导航栏视图的一个片段,我在其中使用了方法 nav_link ,这是我创建的类似于 nav 类的另一个帮助器方法。这是一个水平下拉菜单:
在layouts/application.html.erb中:
<li id="Home" class="<%= nav_class("All") %>"><%= nav_link("All") %>
<ul>
</ul></li>
<li id="Writing" class="<%= nav_class("Writing") %>"><%= nav_link("Writing") %>
<ul>
<li class="<%= nav_class("Educational") %>"><%= nav_link("Educational") %>
由于此方法中的所有内容都弹出为零,我只是这样做了:
def navbar_method(category)
unless Rails.env.test?
c = Category.find_by_name(category)
#see above
end
end
你觉得怎么样?好的?不好吗?
由于我对“不好”投了一票,所以我想我应该多解释一下。由于某种原因,尽管我可能包括固定装置,但该类别在测试中返回为零...我想我可以进行广泛的集成测试来测试导航栏...
I have an application helper which determines the css classes (selected or unselected) and link addresses for the navbar at the top of my application helper.
When I test my controllers, I get a whole bunch of errors regarding the navbar (variables return nil).
Since the navbar has nothing to do with each individual controller, and it appears on every page, the easiest thing to do seems to be this.
In the application helper:
def navbar_method(category)
c = Category.find_by_name(category)
children = c.children.map {|child| child.name}
if category == "All"
if params[:id] == category || params[:category] == category
"on"
else
"off"
end
else
if params[:id] == category || children.include?(params[:id]) || params[:category] == category || children.include?(params[:category])
"on"
else
"off"
end
end
Here's a snippet of the Navbar view where I employ the method nav_link is another helper method I created similar to nav class. It's a horizontal dropdown menu:
In layouts/application.html.erb:
<li id="Home" class="<%= nav_class("All") %>"><%= nav_link("All") %>
<ul>
</ul></li>
<li id="Writing" class="<%= nav_class("Writing") %>"><%= nav_link("Writing") %>
<ul>
<li class="<%= nav_class("Educational") %>"><%= nav_link("Educational") %>
Since everything in this method pops up nil, I just did this:
def navbar_method(category)
unless Rails.env.test?
c = Category.find_by_name(category)
#see above
end
end
What do you think? OK? Not OK?
Since I got one vote for NOT OK I thought I'd explain a little more. For some reason the category is returning nil in the tests despite fixtures I might include...I figured I could do extensive integration tests to test the navbar...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个好主意。
功能测试应该重现与用户请求几乎相同的环境。为什么助手不起作用?
另外,您是否设置了一些单元测试来测试助手?
也许助手本身就有问题。
That's not a good idea.
A functional test should reproduce almost the same environment of an user request. Why the helper doesn't work?
Also, did you setup some unit test to test the helper?
Perhaps the helper itself is buggy.