2013-03-14 11 views
6

Ecco una linea dal mio file test.rb Environnment in un'applicazione Rails 3.1.12:test.host in Rspec Rails test reindirizzare

config.action_mailer.default_url_options = config.action_controller.default_url_options = { :host => "127.0.0.1", :port => 3000 } 

Ora qui è la prova che faccio:

subject { get :success } 
subject.should redirect_to(:home) 

questo produce un errore:

Failure/Error: subject.should redirect_to(:home) 
     Expected response to be a redirect to <http://127.0.0.1:3000/> but was a redirect to <http://test.host/> 

Cosa ho fatto di sbagliato? Oppure, dove è configurato l'host di test?

Ecco il file spec_helper.rb per riferimento completo.

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] = 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 
require 'capybara/rspec' 

# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

RSpec.configure do |config| 
    # ## Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    # Run specs in random order to surface order dependencies. If you find an 
    # order dependency and want to debug it, you can fix the order by providing 
    # the seed, which is printed after each run. 
    #  --seed 1234 
    config.order = "random" 
end 
Capybara.configure do |config| 
    config.app_host = 'http://127.0.0.1' 
    config.server_port = 3000 
end 

risposta

6

Per impostare l'host applicazione e la porta del server con Capybara aggiungere le seguenti righe al file spec/spec_helper.rb

Capybara.configure do |config| 
    config.app_host = 'http://127.0.0.1' 
    config.server_port = 3000 
end 

- Modifica # 1

Una bella panoramica dei domini di test è disponibile all'indirizzo http://blog.joncairns.com/2012/12/testing-domains-with-rails-and-test-unit/

+1

Questo non sembra funzionare per me. Ho aggiornato la domanda con il contenuto dell'helper delle specifiche. – conradkdotcom

+0

Ricevi ancora lo stesso messaggio di errore quando aggiungi le opzioni di configurazione di Capybara? –

+1

Sì. Lo stesso errore. Per aggirare questo, ho usato 'home_path' invece per ora. Ma questo non controlla il dominio, che è un problema a lungo termine. – conradkdotcom

2

Questo ha funzionato per me:

# spec/controllers/whatever_controller_spec.rb 
before :each do 
    @request.host = '127.0.0.1:3000' 
end 

Sulla base del collegamento fornito in dotazione. È estremamente fastidioso dover configurare esattamente gli stessi parametri in config/environments, spec/spec_helper.rb e di nuovo qui ... tutti in modi leggermente diversi (con 'http: //' o senza, con il numero di porta o la porta specificati separatamente). Anche la sintassi Capybara.configure non sembra essere coerente con se stessa tra le versioni ...

Ma provaci e vedi se questo lo risolve.

0

Ecco la soluzione che sembra aver risolto il problema per me:

In spec/rails_helper.rb aggiungere questo

module ActionDispatch 
    class TestRequest 
    # Override host, by default it is test.host 
    def host 
     'localhost:3000' 
    end 
    end 
end