如何将 Rails 助手导入到功能测试中
你好,我最近继承了一个项目,其中前开发人员不熟悉 Rails,并决定将很多重要的逻辑放入视图助手中。
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
include BannersHelper
include UsersHelper
include EventsHelper
end
特别是会话管理。这没问题,并且可以与应用程序一起使用,但我在为此编写测试时遇到问题。
一个具体的例子。某些操作会执行 before_filter
来查看 current_user
是否是管理员。这个 current_user
通常由在我们所有控制器中共享的 sessions_helper
方法设置 因此,为了正确测试我们的控制器,我需要能够使用这个 current_user
方法,
我已经尝试过:
require 'test_helper'
require File.expand_path('../../../app/helpers/sessions_helper.rb', __FILE__)
class AppsControllerTest < ActionController::TestCase
setup do
@app = apps(:one)
current_user = users(:one)
end
test "should create app" do
assert_difference('App.count') do
post :create, :app => @app.attributes
end
end
require 语句找到 session_helper.rb
没问题,但没有 Rails魔法它无法以与AppsControllerTest中相同的方式访问
我如何欺骗这个疯狂的设置来测试?
Hi I recently inherited a project in which the former dev was not familiar with rails, and decided to put a lot of important logic into the view helpers.
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
include BannersHelper
include UsersHelper
include EventsHelper
end
Specifically session management. This is okay and working with the app but I am having problems writing tests for this.
A specific example. Some of the actions do a before_filter
to see if a current_user
is an admin. This current_user
is normally set by a sessions_helper
method that is shared in all our controllers
So in order to properly test our controllers I need to be able to use this current_user
method
I have tried this:
require 'test_helper'
require File.expand_path('../../../app/helpers/sessions_helper.rb', __FILE__)
class AppsControllerTest < ActionController::TestCase
setup do
@app = apps(:one)
current_user = users(:one)
end
test "should create app" do
assert_difference('App.count') do
post :create, :app => @app.attributes
end
end
The require statement finds the session_helper.rb
okay but without the Rails magic it is not accessible in the same way with in the AppsControllerTest
How can I spoof this crazy setup to test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想测试助手,可以按照此处的示例操作:
http://guides.rubyonrails .org/testing.html#testing-helpers
这是针对单个方法的单元测试。我认为,如果您想在更高级别进行测试,并且将使用多个带重定向的控制器,则应该使用集成测试:
http://guides.rubyonrails.org/testing.html#integration-testing
例如:
If you want to test helpers, you can follow the example here:
http://guides.rubyonrails.org/testing.html#testing-helpers
This is for unit tests on individual methods. I think that if you want to test at a higher level, and you will be using multiple controllers w/redirects, you should use an integration test:
http://guides.rubyonrails.org/testing.html#integration-testing
As an example:
我发现的唯一解决方案是重构并使用一个像样的身份验证插件
The only solution i found was to re-factor and use a decent auth plugin
为什么要重构?您可以非常轻松地将项目中的帮助程序包含在测试中。为此,我做了以下操作。
Why re-factor? You can very easily include helpers from your project in your tests. I did the following to do so.
为了能够在测试中使用 Devise,您应该添加
到每个
ActionController::TestCase
实例。然后在setup
方法中,您所做的而不是
所有功能测试都应该可以正常工作。
To be able to use Devise in your tests, you should add
to each
ActionController::TestCase
instance. Then in thesetup
method you doinstead of
All your functional tests should work fine, then.