RSpec controller validation: no template to create

I have an interesting situation. I am testing the following simple create action:

# will only be accessed via Ajax def create click = Click.new(params[:click]) click.save # don't really care whether its success or failure end 

Then I have the following very simple controller specification:

 require 'spec_helper' describe ClicksController, "creating a click" do it "should create a click for event" do xhr :post, :create, :click => {:event_id => 1} # more test to come... end end 

Seems trivial, but I get the following:

Missing template clicks / creation

Any advice would be appreciated.

+4
source share
4 answers

Add to controller action:

 render :nothing => true 

This will automatically create the appropriate server. More here

+10
source

You will get this error if your controller only displays JSON or XML, but you do not specify a format in the specification; your request is then used by default for unsupported HTML. In this case, just specify the supported format when you call the controller method from your specification. For example, change this:

 post :create, registration: @user_hash 

:

 post :create, registration: @user_hash, format: :json 
+4
source

If you do not visualize anything in the controller action, the rails will try to display the template by default (in this case clicks/create ). I would suggest making at least a successful message like this:

render :json => {:success => true}

+2
source

Based on megas answer , if you want to check the action of the controller, access to which can only be accessed through the UJS link, and only the .js.erb template, I'd put this in the controller so as not to interfere with the UJS functionality:

 respond_to do |f| f.html { render nothing: true } # prevents rendering a nonexistent template file f.js # still renders the JavaScript template end 

This will allow you to invoke the controller action by simply calling ActionController::TestCase::Behavior get / post / put / delete instead of calling xhr , because it will successfully call the method, will not display anything, and will not continue, leaving your UJS behavior intact.

+1
source

All Articles