thank_you
thank_you

Reputation: 11107

Rspec test on response should be successful fails

I'm running this test on rspec...

it 'get email is successful' do
  get :email
  response.should be_success
  response.should render_template('email')

end

Where the controller code looks like this..

def email
  respond_to do |format|
     format.js 
  end
end

On my terminal I'm returning with....

3) PostsController checking to see if response for post email is successful
 Failure/Error: response.should be_success
   expected success? to return true, got false
 # ./spec/controllers/posts_controller_spec.rb:116:in `block (3 levels) in <top    
(required)>'

What am I missing here to make the test work? It has to be something obvious. My view file is titled email.js.erb. This action is meant for an AJAX call.

Upvotes: 2

Views: 6996

Answers (2)

ChuckE
ChuckE

Reputation: 5688

try:

xhr :get, :email

your request seems not to take into account the expected response type. In Rspec, this is the way to do it if you want to simulate AJAX (XHR) requests.

Upvotes: 3

Dmitry Frenkel
Dmitry Frenkel

Reputation: 1756

Your controller is structured to return anything only for JS requests and you are requesting HTML response in your tests. You should have

get :email.js

in your spec.

Upvotes: 3

Related Questions