3
votes

I'm trying to set up my ember-cli / rails app with integration testing. After fussing with Ember's built in testing library, I've switched over to using RSpec (which I was using for backend anyway), and Capybara. While I can finally fill in forms correctly, my post request to sign_in always fails. I think the issue is Capybara is posting the request to a different database environment or something! If I check at the rails console, the user is certainly there, and I create a user as part of the RSpec test anyway.

Has anyone managed to set up Ember/Rails/Capybara/RSpec?

This is my spec:

describe "the signin process", :type => :feature, :js => true do
  it "signs me in" do
    visit '/'
    FactoryGirl.create :user, email: "[email protected]", password: 'password'
    within("#tufts-nav") do
      fill_in 'email', :with => '[email protected]'
      fill_in 'password', :with => 'password'
    end
    click_button 'Sign In'
#   here authentication fails mysteriously
    expect(page).to have_content 'Jobs'
  end
end
1
You're going to have to write JS tests somehow (most use phantomjs with Qunit) because of the way ember is built - straight rspec isn't going to work since you're only handling server side transactions.Anthony
Not to be argumentative but this makes me think its possible. Only reason I'm resisting is I've tried Qunit (as its built into Ember), without any luck. Now if I could only understand why requests to the server are failing!Josh Pfosi
Capybara is the important point here. That drives browsers, e.g. selenium or phantomjs. The API is nice and familiar to people who've used it, and if your API / deployment currency is a rails app it makes sense to do acceptance/integration testing like this. Ember's integration testing is probably nice too, but this is just familiar, and if you were to switch Ember for Angular or whatever later on you could keep the acceptance tests.nruth
Are you getting a 401 unauthorized back as the failing response? If so, try binding.pry and ensure the user exists right before sign in (you did mention you checked the console). Also, check the failures in /log/test.log. This was happening to me, and it was caused by my mis-configuration of DatabaseCleaner in my rails_helper.rbRyan

1 Answers

2
votes

Simple/dumb solution

Have RSpec build ember into rails' public/ before your feature specs.

# build ember, hijack rails public/ directory to host ember app
# this way no need to change settings or run any servers
# assumes rails API root path is not used (since ember now hosted from it)
RSpec.configure do |config|
  public_path = Rails.root.join('public')
  config.before(:context, type: :feature) do
    Dir.chdir 'frontend' do
      builder = spawn("ember build --environment=ci -output-path=#{public_path}")
      _pid, status = Process.wait2(builder)
      fail "non-zero exit status #{status}" unless status == 0
    end
  end

  config.after(:context, type: :feature) do
    `git clean -fd #{public_path}`
    `git checkout #{public_path}`
  end
end

Configuring

Our ember-cli app is in rails-root/frontend, you may need to change the name or path to point at yours

You might want to experiment with the environment part, e.g. using production instead. I do this because my production env is hard-coded to target an API we host on heroku but I want the tests to be self-contained, i.e. run against the rails app capybara hosts.

Git is needed for cleanup. If you don't have that you could build to another path and use mv to swap out the rails public/ dir then put it back afterwards.

You may prefer not to have a global install of ember-cli used to build your project (for versioning reasons). If you want to use the project local one point the spawn command at node_modules/ember-cli/bin/ember instead of just ember.

Otherwise, ember-cli-rails

If you're treating the ember app as a component of your rails app, and want to write tests at the rails level (rspec, capybara, etc) then ember-cli-rails is likely a good choice.

This gem handles building the ember app and serving it from the urls you mount it at in your rails routes.

This is transparent to capybara: it sends a request to a ruby webserver and is given back html that calls out to css and js, just like rails normally does.

Be aware there are some issues with assets from ember-cli getting served by rails with the right paths at the moment, which made me switch away to get something deployed quickly. If you're using the rails asset pipeline for css, images and so on, then you shouldn't have a problem. For me, it affected images and webfonts in the ember-cli app.

Apart from that there will need to be a server for the api and a server for the ember front-end (proxying to the rails api), and capybara will need telling to connect to the ember front-end. This Rakefile and this post seem like a start.