简体   繁体   中英

testing rails+backbone application with rspec & capybara

I'm writing a tests for my app with listed gems. I couldn't find how to set Capybara to work with backbone (ajax in all)
example test:

require 'spec_helper' describe "Main" do   describe "GET /" do
    it "displays articles" do
      Article.create!(title:'title',body:'<p>body text</p>')
      visit '/'
      page.should have_content('body text')
    end   
  end 
end

and output of the test:

Failures:

  1) Main GET / displays articles
     Failure/Error: page.should have_content('body text')
       expected there to be text "body text" in "Loading..."
     # ./spec/features/main_spec.rb:8:in `block (3 levels) in <top (required)>'

"Loading..." is the pre ajax text in my view template...

The point is that I don't want to use Jasmine at the moment for this app

It looks like you are running Capybara with the default Rack::Test driver, which means no JavaScript. Rack::Test loads your app in process and fakes a browser DOM behind the Capybara API.

You need to use a driver that runs your tests in a real Web browser. There are a few different options: a Selenium driver comes with Capybara and runs your tests in a real browser (normally Firefox), others are implemented in separate gems, such as Poltergeist which uses PhantomJS and the headless WebKit browser.

There are instructions on the Capybara readme about setting up the right driver. You can switch a group of tests to use a JavaScript supporting driver by adding the js option to a describe block, eg:

describe "Main with JavaScript", :js => true do
  # ...
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM