jeudi 14 mai 2015

Testing Rails Controllers and Middleware- Functional vs. Integration testing?

Background

I have a legacy Rails 3.2 app which I'm converting from a homegrown testing solution to a rails one (currently MiniTest). The app is a JSON api with a single endpoint, so testing is very simple at this point. We build a (relatively complex) json payload and then make assertions against the response.

What I've done so far

The old testing solution was to fire HTTP requests over the wire (using an HTTP gem) at a separately-running instance of the server. This ran it through the entire stack, including the middleware stack, eventually hitting ActionDispatch::ParamsParser which makes the params hash available to controllers.

I converted tests to be subclasses of ActionController::TestCase so that only a single rails instance (the test instance) is running. I then removed the HTTP gem so that I could use the ActionController::TestCase#post method to send the json payload. That's where things fell apart

The Problem

It seems like only rails integration tests exercise the middleware stack, while functional tests are more like unit tests for the controller. Thus, behind ActionController::TestCase#post is the #paramify_values method, which looks like it's mocking out the params hash with some logic that's like the logic in ParamsParser but has some differences. Most importantly, when I try to send a float in my params hash, it gets converted to a string. ParamsParser correctly put the float in params. I do a bunch of math in my controller, but math doesn't work so well when you're getting passed (incorrectly) a string, instead of a number.

My Question(s)

  • Is there a way to run an ActionController::TestCase with the rails middleware stack handling things?
  • Am I using the wrong tool, and should instead be using ActionDispatch::IntegrationTest? The examples on the rails guides for ActionDispatch::IntegrationTest look like they're made for user workflows, more akin to cucumber/capybara scripts than firing single requests.

Aucun commentaire:

Enregistrer un commentaire