Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails controller test, getting ActionView::MissingTemplate: Missing template

I figured I should finally write some tests for my rails app.

My controller is "UsersController". It doesn't have any HTML as I just have an iphone app sending a post in to a rails controller.

Here is my test:

require 'test_helper'

class UsersControllerTest < ActionController::TestCase


  def test_create
    # to http post
    # /users
    #user[email]=%@&user[password]=%@&user[password_confirmation]=%@
    #post
    post(:create, :user => {:password => "testpassword", :password_confirmation => "testpassword"})
  end

Problem is that I get this error:

1) Error: test_create(UsersControllerTest): ActionView::MissingTemplate: Missing template users/new with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths

So I guess it's trying to populate an HTML page? If so, I find this odd. I would think it would directly do the post to the controller. Can someone confirm that this "post" method tries and populates an HTML form?

If this is the case, how should I proceed in writing a test to directly send an HTTP post to the controller?

Thanks for any help

like image 601
phil swenson Avatar asked Oct 18 '25 12:10

phil swenson


2 Answers

You can specify "format" to make it work:

post(:action, {'param1'=>'value1', 'param2' => 'value2', :format => 'js'})
like image 71
Yosep Kim Avatar answered Oct 21 '25 03:10

Yosep Kim


Unless you tell it otherwise the post method assumes the requested content type is HTML. Typically the create action looks something like this:

@user = User.new(params[:user])
if @user.save
  redirect_to posts_path
else
  render :new
end

If the action fails it tries to render 'users/new', which doesn't exist, thus the error.

So there are a couple of issues here. It's not clear what content type is expected (XML?). The action is failing, but we can't see why. It might help to edit the question to show us what the controller action does.

like image 41
zetetic Avatar answered Oct 21 '25 04:10

zetetic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!