Running capybara with nginx - ruby-on-rails

Is is possible to run capybara with nginx and passenger? instead or webrick? Capybara is installed with cucumber in a rails app.

It is easy - the only thing you have to do is to switch your mind - neither capybara nor cucumber are not tied to local environment you can test application that is located in internet and it will not care about it - you can even test google.com if you want.
For your particular problem you'll have to set
Capybara.run_server = false
Capybara.server_port = 8000 # or whatever port is your instance of nginx is configured to serve
Capybara.app_host = 'http://www.google.com' # if your instance is running on remote machine, else just drop it and capybara will use localhost
You can easily control restarting of your application using cucumber hooks, you can configure it to restart before each test or before test suite. (See cucumber wiki) Within hook you'll have to issue FileUtils.touch tmp/restart.txt command. The same with database - you can manually setup hook to truncate it whenever it is needed (See database_cleaner gem)

Related

Rspec using test, Capybara using development database

I have an interesting problem. I'm using Rspec for test driven development and Capybara with Poltergeist for acceptance testing. Oh, and FactoryGirl, too. Rspec and FactoryGirl is operating against the test database, which is what I want. The problem is that acceptance tests are operating against the development database.
This causes simple tests like the following to fail:
my_class = FactoryGirl.create(:my_class)
visit my_classes_path
expect(page).to have_content(my_class.title)
I've even checked screenshots along the way using:
page.save_screenshot("screenshot#{__FILE__}_#{__LINE__}.png")
SOLUTION
So apparently Capybara was attempting to use the same URL and port that is initialized in my local environment when I kickoff "rails server". Specifying a different port in my Capybara configuration did the trick as seen below:
Capybara.configure do |c|
c.run_server = true
c.javascript_driver = :poltergeist
c.default_driver = :poltergeist
c.server_port = 7000
c.app_host = "http://localhost:#{c.server_port}"
end
For normal use you shouldn't have to lock to a specific port or set app_host. If app_host isn't set then Capybara defaults to http://#{Capybara.server_host}:#{Capybara.server_port} which by default is http://127.0.0.1:<the port capybara runs the server on> . If you need to use localhost instead of 127.0.0.1 (because of IPv6 or something) then just set
Capybara.server_host = 'localhost'
instead of app host and fixing the port. app_host is really for when you're trying to test an external site, or you need to access subdomains to test your app - and fixing the port is really intended for issues with firewalls, etc.

What should I default_url_options[:host] for integration testing, if using Pow?

I'm using Pow to serve up my development environment. In my config/environments/development.rb and test.rb files, I had this line:
Rails.application.routes.default_url_options[:host] = "myapp.dev"
But of course that doesn't work, because then my RSpec/Capy integration test run against the development database and don't match the factory data.
Why does this even matter? I thought the testing suite spun up its own Rack server. Since it does appear to matter, to what do I set it?
Change the port in your spec_helper.rb for starters, as you're apparently running tests in your development environment.
Capybara.run_server = true
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"

Setting an environment variable when running default Debug command in Rubymine

I have feature specs in my Rails app that use Capybara to run tests through a browser. By default they use the phantomjs/poltergeist and rack-test drivers, which are headless.
It often is desirable to run a single spec (or spec file) using a capybara driver that runs a visible browser so that I can see what is happening. For this use case I use Selenium. When I want to run a spec using Selenium I set an environment variable (DEBUG=true) before running rspec like so:
DEBUG=true rspec spec/features/my_spec.rb
My spec_helper looks for the DEBUG environment variable and runs the spec with Selenium instead of the default drivers. I'd like to duplicate this capability in Rubymine, so that when I right-click on a spec in the project tree and click "Debug 'Run spec...'" it sets up the DEBUG environment variable and runs the spec with the Selenium driver. How can I configure the default 'Debug' configuration to set up this environment variable?
Next to the run button on the tool bar, there's the name of the configuration that is active, click it and choose Edit Configuration inside you'll find server arguments you can write there the DEBUG=true and it will be sent to the runner file.

Rails Test Environment Named Routes

I've noticed that when I run tests (using Rspec and spork, if that matters), my hostname is set to www.example.com. So if I do:
visit sports_url
the test is actually going to www.example.com/sports.
This is a problem because I need to run some of the tests with selenium (to get javascript support). Also, I noticed my emails where being marked as coming from www.example.com.
What did I mess up? Why is the test environment running as example.com? Can I configure this somewhere? I would assume it should be programatic (depending on what port the test server starts up on).
You can configure the test environment domain, then set up DNS to do what you need.
How do I change the default "www.example.com" domain for testing in rails?

Port of the Rails app when running Cucumber tests

Is there a way to get, in the test, the port in which the rails app is running during cucumber test? I tried Capybara.server_port but that's nil.
Thanks.
When using the selenium driver, the port can be found on:
Capybara.current_session.driver.rack_server.port
and when using the webkit driver, it can be found on:
Capybara.current_session.driver.server_port
Alternative, you can set
Capybara.server_port
to a known value and use that.
My understanding is that if you're using rack-test, the default Capybara driver, then there's isn't actually any real web server running to make requests to.
If you want to view your app as Cucumber/Capybara would, then you'd need to start it up manually on a chosen port:
$ RAILS_ENV=test rails s -p 4000
And then have something like this in env.rb:
Capybara.configure do |config|
config.run_server = false
config.app_host = "http://localhost:4000"
end

Resources