【发布时间】:2016-08-01 01:16:13
【问题描述】:
我一直在寻找如何为 Devise 4.1.1 注册控制器设置测试,我知道不建议这样做,但对于我的应用程序,注册控制器已特别定制以满足我们的需求。在注册过程中,用户表中只有几个新列被保存,所以我不认为它会影响测试环境。
我也知道可以进行集成测试来实现这一点,但是我现在希望通过单元测试来做到这一点。
我已经按照 Devise 的建议在 test_helper.rb 中设置了这个部分
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
#This is what I added:
class ActionController::TestCase
include Devise::TestHelpers
end
我在文件夹 controllers/users/ 中创建了一个名为 registrations_controller.rb 的新文件,因为它尚不存在,内容如下:
require 'test_helper'
class Users::RegistrationsControllerTest < ActionController::TestCase
setup do
@request.env["devise.mapping"] = Devise.mappings[:user]
end
test "sign_up view" do
get :new
assert_response :success
flunk("test flunk")
end
end
运行rake test时,输出如下:
$ rake test
(in /home/www/dev)
Run options: --seed 163
# Running:
Finished in 0.010214s, 0.0000 runs/s, 0.0000 assertions/s.
0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
我在这里错过了什么??
【问题讨论】:
标签: ruby unit-testing ruby-on-rails-4 devise testunit