Friday, July 18, 2008

RSpec:Controllers:can the 'have_tag' be used in Controller testing?

Other sources indicate that one should be getting back html content in the response.body attribute:
- Action Pack 0.7.5: On rails from request to response

* Easy testing of both controller and template result through
TestRequest/Response

class LoginControllerTest < Test::Unit::TestCase
def setup
@request = ActionController::TestRequest.new
@request.host = "http://somewhere"
end

def test_succesful_authentication
@request.action = "authenticate"
@request.request_parameters["user_name"] = "david"
@request.request_parameters["password"] = "secret"

response = LoginController.process_test(@request)

assert_equal(
"http://somewhere/clients/", response.headers["location"])
assert_equal Person.find(1), response.session["person"]
assert(response.body.split("\n").include?(
"<h1>You've been logged in!</h1>"))
end
end

Learn more in link:classes/ActionController/TestRequest.html


It appears that this code snippet from a controller test (even though it's not RSpec) is able to get html from the response.body attribute.

2 comments:

Silent Sentinel said...

Check on the section on response.should redirect_to.

RSpec-1.1.4: Controllers

It appears there is documentation supporting the fact that html should be available when testing a controller action.

Silent Sentinel said...

Short answer: yes

Longer answer: see RSpec MVC boundaries: why I was getting back template locations instead of html