[英]How can I test integration with RSpec and Devise
I have quite some issues with testing the features of my rails application using RSpec. 使用RSpec测试我的rails应用程序的功能时,我遇到很多问题。 I have a company controller, and are using devise to make sure that you need to be logged in to access the controller.
我有一个公司控制器,并且正在使用devise来确保您需要登录才能访问控制器。
I then have integration tests like the following: 然后,我将进行如下所示的集成测试:
describe "with valid information" do
before do
fill_in "company_address", with: "My special address"
fill_in "company_name", with: "My User"
fill_in "company_contact_email", with: "test@test.com"
fill_in "company_contact_name", with: "Someone Hello"
end
it "should create a company" do
expect { click_button submit }.to change(Company, :count).by(1)
end
end
It, of course, fails because I don't have a user who is logged in. I have tried creating a test helper like described here, and it works well for controllers, but not for integration tests. 当然,它失败了,因为我没有登录的用户。我曾尝试创建一个如此处所述的测试助手,它对于控制器非常有效,但不适用于集成测试。
What is the best way to log a user in with Devise, prior to these tests? 在进行这些测试之前,用Devise登录用户的最佳方法是什么?
add file devise.rb
to rspec/support
And here is content 将文件
devise.rb
添加到rspec/support
,这是内容
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
Another way to solve your problem: Rails integration test with the devise gem 解决问题的另一种方法: 使用devise gem进行Rails集成测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.