正如標題中所解釋的那樣。設計登錄在rspec測試中不起作用
這裏是我的軌道幫手:
0 ENV['RAILS_ENV'] ||= 'test'
9
8 require File.expand_path('../../config/environment', __FILE__)
7 require 'spec_helper'
6 require 'rspec/rails'
5 require 'capybara/rspec'
4 require 'capybara-screenshot/rspec'
3 require 'capybara-screenshot/minitest'
2 require 'devise'
1 include Capybara::DSL
11 \
1 abort("The Rails environment is running in production mode!") if Rails.env.production?
2
3 ActiveRecord::Migration.maintain_test_schema!
4
5 RSpec.configure do |config|
6 config.fixture_path = "#{::Rails.root}/spec/fixtures"
7 config.include Devise::TestHelpers, :type => :controller
8 config.extend ControllerMacros, :type => :controller
9 config.include Capybara::DSL
10 config.use_transactional_fixtures = true
11 config.infer_spec_type_from_file_location!
12 end
這是我acctual測試:
1 require 'rails_helper'
1
2 describe DashboardController, type: :controller do
3
4 it 'should display chart', js: true do
5 @request.env["devise.mapping"] = Devise.mappings[:user]
6 member = Fabricate(:member)
7 member.confirm
8 domain = Fabricate(:domain, member: member)
9 Fabricate(:url, domain: domain, url: '/ayoo_lmao', time_spent: 122)
10 Fabricate(:url, domain: domain, url: '/ayoo_lmao', time_spent: 125)
11
12 sign_in :member, member
13
14 visit root_path()
15
16 binding.pry
17 end
18 end
所以,當我運行這個測試,火狐打開並顯示我的登錄頁面(但我沒有alredy sign_in )。當我manualy嘗試sign_in槽binding.pry並參觀root_path我得到:
[2] pry(#<RSpec::ExampleGroups::DashboardController>)> visit root_path
Errno::ECONNREFUSED: Connection refused - connect(2) for "127.0.0.1" port 7055
from /home/karlis/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `initialize'
我member_fabricator.rb:
2 Fabricator(:member) do
1 email '[email protected]'
3 password 'rootroot'
1 first_name 'Rinholds'
2 last_name 'Jordan'
3 expiration Time.local(2015, 6, 21, 15, 13, 0)
4 start_date Time.local(2015, 4, 21, 15, 13, 0)
5 role 2
6 end
如何在RSpec的控制器測試一個使用sign_in?
謝謝你。
最後一個錯誤是無效的,因爲我關閉了Firefox驅動程序,它返回「」,當我訪問root_path()。 – IvRRimUm
你的spec/fabricator/user_fabricator.rb文件是什麼樣子的? –
我用member_fabricator.rb文件內容更新了這個問題。 – IvRRimUm