Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert the content of the response body in rails 3 functional test

I have a Rails 3.0.7 application. In the functional test I have following code. I am not using rspec or anything. This is plain vanilla functional test that comes with rails.

assert_response :success

Above assertion is passing. However I need to assert on the full content that is returned as the body of the response. How do I access the response body?

like image 829
Nick Vanderbilt Avatar asked Jun 06 '11 23:06

Nick Vanderbilt


People also ask

What is Minitest in Ruby on Rails?

What is Minitest? Minitest is a testing tool for Ruby that provides a complete suite of testing facilities. It also supports behaviour-driven development, mocking and benchmarking. With the release of Ruby 1.9, it was added to Ruby's standard library, which increased its popularity.

How do you run a Minitest in Rails?

To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.

How do I run a test case in Rails?

Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case. You can also run a particular test method from the test case by providing the -n or --name flag and the test's method name.


2 Answers

You can access the response body in a functional test via:

response.body

Often, in this style of testing, you really want to use the method:

assert_select

A nice way to check out the assert_select API is via the cheat gem:

gem install cheat
cheat assert_select
like image 191
Dan Croak Avatar answered Sep 19 '22 13:09

Dan Croak


This rails testing guide might help you some: http://guides.rubyonrails.org/testing.html#testing-views

like image 22
andrewmitchell Avatar answered Sep 21 '22 13:09

andrewmitchell