views:

345

answers:

2

Say I have a Metal class named Preview. How do I test it with RSpec?

When I try:

require 'spec_helper'

describe Preview do

  it "should return the posted content" do
    post "/preview", :content => "*title*"
    response.body.should == "*title*"
  end

end

I get:

undefined method `post' for #<ActiveSupport::TestCase::Subclass_1:0x1058b3098>

It seems that RSpec doesn't load up the :post method if the test isn't explicitly for a Controller. I've tried specifying :type => :controller to no avail.

A: 

I think it's better to test with rack/test

shingara
Looks good, but I don't want to use another test suite/gem just to test one route. I'm sure you understand.
Matt Darby
+1  A: 

Matt - rspec has integration specs that wrap rails integration tests (which go through rack). Just put that spec in ./spec/integration/preview_spec.rb.

HTH, David

David Chelimsky
Bingo. Is this method written up anywhere? Thanks for RSpec and all your work David.
Matt Darby