2017.10-28 完整的文章看这里: http://siwei.me/blog/posts/rails-rspec-add-rspec-to-rails-and-test-lib-files
refer to: https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
上面的链接已经介绍的很详细了。
但是看懂的话需要: rspec 知识, module 知识,rails 知识, devise 知识。
呃,所以,对于想快速入门的同学,这样做就好了:
0. 建立一个 Admin模型。 如果你是默认的Devise, 那就把下面的Admin都替换成 User.
1. rails_helper中:
RSpec.configure do |config| # 增加这一行: config.include Devise::TestHelpers, :type => :controller end def admin_login sign_in Admin.create(email: '[email protected]', password: '88888888') end spec/rails_help
spec :
# -*- encoding : utf-8 -*-
require 'rails_helper'
describe SystemSettingsController do
before do
admin_login
end
it 'should get index' do
get :index
response.should be_success
end
end