2013-11-25 2 views
4

Я использую rspec, capybara и launchy для тестирования своего веб-приложения.Активы не загружаются во время capybara/rspec spec

Вот моя спецификация:

require 'spec_helper' 

describe "Routes" do 
    describe "GET requests" do 
     it "GET /root_path" do 
      visit root_path 
     page.should have_content("All of our statuses") 
     click_link "Post a New Status" 
     page.should have_content("New status") 
     fill_in "status_name", with: "Jimmy balooney" 
     fill_in "status_content", with: "Oh my god I am going insaaaaaaaaane!!!" 
     click_button "Create Status" 
     page.should have_content("Status was successfully created.") 
     click_link "Statuses" 
     page.should have_content("All of our statuses") 
     page.should have_content("Jimmy balooney") 
     page.should have_content("Oh my god I am going insaaaaaaaaane!!! ") 
     save_and_open_page 
     end 
    end 
end 

Мой .rspec

--color 
--order default 

и мой spec_helper.rb:

# 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 } 

# Checks for pending migrations before tests are run. 
# If you are not using ActiveRecord, you can remove this line. 
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) 

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 
    config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    DatabaseCleaner.clean 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 

    # 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 

Если вы посмотрите на моей спецификации, вы увидите спецификацию rspec, которая использует capybara для просмотра моего приложения, и заканчивает, вызывая метод save_and_open_page для запуска stary gem, чтобы открыть этот окончательный pa ge в браузере для человека, на который можно смотреть. На этой последней странице, однако, не отображается javascript или css, а только чистый HTML.

Есть ли у кого-нибудь идеи, почему это было бы? Я хочу протестировать javascript и предпочел бы, чтобы все активы были загружены.

ответ

9

Внутри config.before(:suite) do

добавить:

%x[bundle exec rake assets:precompile] 

прекомпилировать ваше Rails активов, то в вашей test.rb среды файл оный:

config.action_controller.asset_host = "file://#{::Rails.root}/public" 
config.assets.prefix = 'assets_test' 

, чтобы указать на местоположение скомпилированных активов. Теперь вы можете использовать активы при запуске Capybara. Примечание: убедитесь, что вы используете git, чтобы игнорировать эту новую папку.

+0

Будет ли это сделать каждый индивидуальный тест медленнее? –

+0

Знаете ли вы, как это сделать, если не используете RSpec? – sscirrus

0

Вы можете просто добавить в test.rb:

config.assets.compile = true 
Смежные вопросы