How can I test Rack middleware? - ruby-on-rails

I'm writing a middleware that does something like this
Given I am logged in
When I visit home page
Then the request and all of my user information should get logged into CouchDB
This is basically it, the middleware itself isn't that complicated, but I'm having trouble with the workflow.
First thing is, that I have no idea how to test something like this. The feature itself is pretty clear, but how would I go about implementing it? Probably the highest level test that I can do is send a request via curl and then check if it got saved into CouchDB.
The problem is, I'm not really sure at what level should I test this and what types of tests will be most helpful. At the moment I basically hit F5 and take a look in the db if there is a new record.
The app is running on Rails 3.2.2 and I'm using couchrest gem to do the logging.

This seems like the place for integration tests. If the stock rails integration tests bypass your middleware treat it like a rack app and use [ https://github.com/brynary/rack-test ]

This Sinatra tutorial ought to help:
http://www.sinatrarb.com/testing.html
You probably also want to redirect logging for your middleware to the Rails logger in an initializer so you can see what's going on while your tests are running:
My::Awesome::Middleware.logger = Rails.logger

Related

Changing the value of a session variable in RSpec/Capybara/Selenium

I'm starting to write feature specs for a Rails application using Rspec with Capybara and Selenium to drive the browser.
While executing one of the specs I want to change the value of a session variable. Eg: I want to set session[:user_id]=123 so that I can test features in my application without having to go via the login screen every time.
When using Capybara with the default rack_test driver, the rack_session_access gem works for accessing the session. But it doesn't seem to work when using the Selenium driver.
And yes, this question has been asked before, but no satisfactory answer has been given.
Short answer, you don't. Longer answer, that's completely against the point of feature tests which are meant to be end to end.
That being said, if for performance reasons you want to short circuit the login, most authentication libraries provide a test mode that will allow you to do that - devise via the Warden TestHelpers for instance

How can I use VCR with Rails 5.1 system tests?

Many things on the web seem to suggest that VCR can be used with Capybara.
I have three problems.
This doesn't make much sense to me, because the test driver and the application code don't share memory.
I'm not finding full recipes on how to set this up.
I'm finding bits and pieces of how people have set this up, but it's outside of the context of rails 5.1, which does the capybara setup behind the scenes.
How do I configure a Rails 5.1 app, Capybara, and VCR to work together for system tests?
(My headless browser is phantomjs, driven by poltergeist. But I don't need to intercept requests from the browser, only server-side requests. If I needed to intercept from the browser I would probably use a full http proxy server, like puffing-billy.)
I'm assuming you mean Rails 5.1 since Rails 5 doesn't have system tests.
The copy of the application Capybara runs for testing is run in a separate thread, not a separate process. This means they do have access to the same memory, and loaded classes
There is nothing special required for configuring WebMock or VCR beyond what their READMEs already provide
The setup of Capybara and how Rails handles it is irrelevant to the configuration of WebMock or VCR. Additionally, even when using Rails 5.1 system tests all of the normal Capybara configuration options are still usable.
That all being said, there are a couple of things to be aware of here. Firstly, WebMock/VCR can only deal with requests made by your app (not from the browser which you stated you don't need) and it's generally better to use faked services (if possible) rather than WebMock/VCR when doing end to end system tests since there is less interference with the code under test.
If this doesn't answer your issues, post a question with a specific issue you're having, the code that's causing your issue, and the error you're getting.

Using PhantomJS in Ruby on Rails application

I would like to use PhantomJS as part of my main application lifecycle to take screenshots of a remote URL submitted by the user.
I'm familiar with using Poltergeist in conjunction with Capybara/Rspec. But how would I go about initializing the page object manually?
To initialize a capybara session in your app you can just do something like
session = Capybara::Session.new(:poltergeist)
( as documented here) and then rather than using page just call Capybara methods on session. One thing to note is that if you're going to test the app with Capybara too you will probably want to register a separate driver for the app and testing - https://github.com/jnicklas/capybara#configuring-and-adding-drivers . Also since Capybaras config is not thread-safe changing any of Capybaras setting would potentially affect both the test session and the in app session.
A far better solution may be to setup a separate Node.js service which runs phantom.js - in fact there are quite a few projects that provide a ready made screen capture webserver / console command.
Capybara is a testing tool and invoking a javascript runtime via ruby adds tons of overhead as well as not being thread-safe. The fact that it is not designed to be run in production is also a pretty big concern.
Instead you would simply call your screenshot service via HTTP or by running a shell command from Ruby.
I really like phantomjs in Rails app.
My suggest are using:
watir (https://github.com/watir/watir)
phantomjs (http://phantomjs.org/download.html)
You can take a screen shot very easy by using follow this: http://watir.github.io/docs/screenshots/
And if you want to use Page, i thinks you should see PageObject in here: https://github.com/watir/watir/wiki/Page-Objects

How to test a Rails HTTP request to a Sinatra app?

Consider a Rails app that hits a (Sinatra app) API being developed separately from the Rails app. I want to test an API call from within the Rails tests.
The API code:
post '/foo/create' do
...
I created a mock, but that doesn't make sense because it is just a copy of the API file. That stinks.
It is possible to require the API file in the test. But how to call it from RSpec? There is no route in the Rails app for it.
One option is to start the API and make the HTTP call from the Rails test, but this is smelly because:
You have to start the API server to run the Rails tests
Why should a Rails test make a HTTP request? Rack::Test simulates this.
I don't think this will work because the apps have different test databases, but share the same production database.
EDIT: The point of the test is that the API call creates records that the Rails app is expecting. So the Rails app needs to test the state of the database after the API call is made.
Well. The perfect answer for you is a gem to mock the answer like webmock. It will fake a response when acessing that url, so on the test your app will make the requisition as it was for real, only that before it hits the web, it will hit your mock and respond with the desired answer.

Making remote web requests in rspec

I have a php app that is going through a migration to rails. We are currently sharing the session from php to rails using memcached. The problem that has arisen is now testing has become difficult. I can't make remote requests using capybara to write functional that really test the full application.
The only solution I have come up with is mocking out the memcached communication, which isn't what I really want in integration testing.
Does anyone have any better options for this? I've tried other capybara drivers, such as mechanize, but I can't get external pages to load.
Have you tired Culerity driver?: Capybara::Session documentation
# NB! untested!
require 'capybara/session'
session = Capybara::Session.new(:culerity)
session.visit('http://www.google.com')
session.fill_in('q', :with => 'Capybara')
session.click_button('Search')
session.should have_content('Capybara')

Resources