Rails Test Environment Named Routes - ruby-on-rails

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?

Related

rails Action Mailer perform_deliveries=false is not working

I am running rails 5.1.0
In config/environments/test, I have config.action_mailer.perform_deliveries = false.
I am running a rails server with -e test to perform end to end to tests.
When I switch the server back to the dev environment, it loads a new browser tab rendering every email that wasn't sent during my tests. If I run my tests 5-6 times, this results in 50 browser tabs opening at once when I switch back to development.
For perform_deliveries Rails docs say:
If this value is false, deliveries array will not be populated even if
delivery_method is :test.
I am also running sidekiq and redis, so I wasn't sure if I have to somehow set the environment on those.
Is there a way for me to completely prevent emails from being queued into the system in my testing envrionment?
You can clear the sidekiq queue after you have run the server in the test environment. Or use other queue name in the test environment.
But the main thing is you should never run server in this mode.
For test you should use unit-tests or better rspec.

server is undefined in ember cli acceptance test

I am trying to set up an acceptance test for an ember application that is using ember-cli-mirage
The doc says that server is supposed to be a global if you are in an acceptance test, but that is not working for me.
You can see the test/code here https://github.com/chrisortman/ember-cms-frontend/blob/master/tests/acceptance/consent-document-test.js#L7
I managed to make it start working.
I had to qualify my usage of server with window.server
https://github.com/chrisortman/ember-cms-frontend/commit/548189a18e472402def2b69881219e7c2519e0e5
EDIT
It works when I run the tests in browser with the /tests url, but not when using ember test or ember test --server
EDIT2
Seems due to a bug, but if you specify something for your host values it works
https://github.com/chrisortman/ember-cms-frontend/commit/745e28c3feafeee8b5f7f0faa7a554b0a364fd52

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.

How can I test my production environment instead of my test environment Rails

I've been struggling with a problem for a few days right now and I want to know how I can force my Rails app to run in environment and then I want to be able to test the images to ensure that they are http or https.
I'm getting closer and closer to solving the problem but now I want to test my production site instead of my test. The reason being is that when I run my rake tests to check to see if the images have either http or https, it only gives me a relative link such as
/images/9995/0007/company_logo.png
This is not helpful to me at all. In order to assert_match /http:/ patten, I need it to grab the images on the production site not in test. How can I do this? I've been researching this for a very long while now and I still feel that I'm no where close to figuring this out.
I've tried to force the Rails environment into production by placing this line of code into config/environments.rb
ENV["RAILS ENV"] ||= 'production'
THe problem is, when I run my tests, doesn't it still refer to the test database? How do I know that when I run rake test TEST=test/functional/ect_test.rb its running production?
I've also tried to have my database.yml file to "point" to production but I made a huge mess of this and to quite frankly I don't really know how that works (It was a recommendation from another stack overflow site). Anyways, some help would be greatly appreciated.
*FYI I am running on Rails 2.3. We are in the middle of upgrading our database. Any input would be greatly appreciated. Thanks.
It's just a matter of starting your server or running your tests with the environment set to production. To do this just prepend RAILS_ENV=production to the commands rails server or rake test or whatever you run you server or tests with. The line:
ENV["RAILS ENV"] ||= 'production'
Is misspelled (just mentioning in case). There's an underscore missing in it. Also, it won't set the RAILS_ENV to production if that ENV value is already set. Make it:
ENV["RAILS_ENV"] = 'production'
To ensure you overwrite any value. Although the aforementioned command prepend technique should work.
Also, if your site runs in https you can be sure the images with relative paths will be served via https as well.

Running capybara with nginx

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)

Resources