Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MissingTemplate while testing controller action using rabl. #446

Open
puneettrehan opened this issue May 4, 2013 · 6 comments
Open

MissingTemplate while testing controller action using rabl. #446

puneettrehan opened this issue May 4, 2013 · 6 comments

Comments

@puneettrehan
Copy link

I have controller action whose output i am rendering using rabl.

Controller looks like this
class Api::V1::CategoryController < Api::ApiController

def index
@categories = Category.all()
end
end

My rabl templates are like this
show.json.rabl

object @category
attributes :id, :description
node (:pic_urls) { |category| get_category_pic_urls(category)
}

index.json.rabl
collection @categories

extends "api/v1/category/show"

When i call this controller action in browser it renders json nicely.

But when i am trying to run my rspec test it fails with error. My category_spec.rb is like
require 'spec_helper'

describe Api::V1::CategoryController do
render_views

it "should return with an HTTP 200 response" do
    #categories = FactoryGirl.build_list(:category_with_pictures)
    categories = Category.all
    Category.stub(:all).and_return(categories)

    get :index
    expect(response.status).to eq(200)
end

end

This is error that i am getting

Failure/Error: get :index
ActionView::MissingTemplate:
Missing template api/v1/category/index, api/api/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml, :rabl]}. Searched in:
* "/Users/macuser/code/goalchi/app/views"

My question is why while running test it is not able to get this template.
i am on rails Rails 3.2.13.

Thanks

@carhartl
Copy link

carhartl commented May 4, 2013

I found, that whenever there is an actual error in the view, say trying to access an attribute that doesn't exist, it would produce the missing template error. Gave me much headache before I found out about this. Maybe that's a hint.

@sricc
Copy link

sricc commented Jul 23, 2013

I'm having the same issue.

I'm using:
Rails 4
Minitest::spec

Everything works fine through a REST client or the browser, but keep getting 'Missing template' when I try and test my controllers. I've double and triple checked my templates (which do exist) and everything seems fine.

Thanks.

@puneettrehan
Copy link
Author

I was facing the same error. This worked for me

it "should get recent public goals" do
get :index, :format => :json
resp = JSON.parse(response.body)

  #Validate the status code OK=200.
  response.status.should == 200

end

If you look at documentation of get action second parameter is format. I was missing that parameter that was causing missing template error.

@sricc
Copy link

sricc commented Jul 24, 2013

That did it! Thanks, missed that as well.

@f-moya
Copy link

f-moya commented Jul 6, 2016

I know it's been 3 years since this post, but i am new in rails and i'm suffering this issue. The solution

"should get recent public goals" do
get :index, :format => :json

worked fine for the index, but when i am at the update teste, the error is still there.

 ActionView::MissingTemplate
       Missing template customers/update, application/update with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :rabl]}.

This is my controller

def update
    @updated = Customer.update(customer_params[:id],customer_params)
 end

and this is my test;

describe "update" do
    it "updates the requested customer" do
      @customer = FactoryGirl.create(:customer)
      patch :update, id:@customer.id,customer:{id:@customer.id,recipient:"Angie"}
      @customer.reload
      expect(@customer.recipient).to eq("Angie")
    end
 end

Thanks

@f-moya
Copy link

f-moya commented Jul 6, 2016

The problem was that i didn't have any idea of where to put the param :format, after a while i found it :

describe 'update' do
    it 'updates the requested customer' do
      @customer = FactoryGirl.create(:customer)

      patch :update, format:'json',id:@customer.id,
          customer:{id:@customer.id,recipient:'Angie'}

      @customer.reload
      expect(@customer.recipient).to eq('Angie')
    end
  end

Now it seems silly know where to put the param, but it wasn't so obvios for me time ago, so i will leave the comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants