ccocchi / rabl-rails

Rails 4.2+ templating system with JSON, XML and Plist support.
MIT License
209 stars 51 forks source link

Error Testing with Rspec #16

Closed Tonkpils closed 11 years ago

Tonkpils commented 11 years ago

I'm testing the responses with rspec. I previously had set up the views with the rabl gem but decided to try rabl-rails. All my tests were passing on using the rabl gem but once I made the switch to rabl-rails the tests started failing.

The weird thing is that they fail randomly and depending on the seed of the tests. I get the following error:

Failure/Error: post :list
     ActionView::Template::Error:
       can't convert nil into String
     # (eval):13:in `compile_source'
     # ./app/views/posts/index.json.rabl:2:in `_app_views_posts_index_json_rabl___1097657160476041507_70197771010180'
     # ./app/controllers/api/v1/posts_controller.rb:300:in `list'
     # ./spec/controllers/api/v1/posts_controller_spec.rb:218:in `block (4 levels) in <top (required)>'

Here's my view for that specific call:

object false
node(:posts) { partial('posts/_show', object: @posts) }
extends 'shared/success'

All my tests fail with that same error but the line # (eval):13:in 'compile_source' has different line numbers on different views.

Also, it is very random. Depending on the order of the tests, different views will fail but the errors are always the same. Any idea why this is happening?

Edit Another thing to point out is that not all the views fail depending on the order. Certain views will fail sometimes but others will pass.

Tonkpils commented 11 years ago

It seems the failure is in the extends 'shared/success' I'm not sure what is causing it though but here's the code in that partial and I'm not sure why it would cause that.

object false

node(:status) { :success }
node(:message) { @message }
ccocchi commented 11 years ago

The ActionView error output is pretty much useless here. Can you reproduce this issue in a small app?

fnordfish commented 11 years ago

This actually rings a bell - It's exactly what happened to me in #15. Sorry, I just couldn't reproduce the exact error.

Tonkpils commented 11 years ago

@fnordfish @ccocchi I actually managed to reproduce the same error in a completely different app. Without even running tests. Here's what happened. I made two routes. GET /users = > "users#index"

Here's the code for index:

  def index
    @users = User.all
    @message = "Testing index"
    render 'users/index'
  end

Here's the code for the views: index.json.rabl

object false
node(:users) { partial('users/_show', object: @users)}
extends 'shared/success'

_show.json.rabl

attributes :id, :first_name, :last_name, :email

shared/success.json.rabl

object false
node(:status) { :success }
node(:message) { @message }

This action actually displays everything correctly with the JSON when called from the browser the output is like such:

{"users":[{"id":18,"first_name":"leo","last_name":"correa","email":"lcorr005@gmail.com"}],"status":"success","message":"Testing index"}

Where I can reproduce the issue is here, I have another route called GET /private/user => "users#show"

show action

def show
  @user = User.find(18)
  @message = "Hello there"
  render 'users/show'
end

show.json.rabl

object false
node(:user) { partial('users/show', object: @user) }
extends 'shared/success'

Error I get is the same:

TypeError in Users#show

Showing /Users/leo/some_app/app/views/users/show.json.rabl where line #2 raised:

can't convert nil into String
Extracted source (around line #2):

1: object false
2: 
3: node(:user) { partial('users/show', object: @user) }
4: 
5: extends 'shared/success'
Rails.root: /Users/leo/work/cloud/cloudid

Application Trace | Framework Trace | Full Trace
app/views/users/show.json.rabl:2:in `_app_views_users_show_json_rabl__4099721713394439804_70329454872940'
app/controllers/users_controller.rb:43:in `show'

This is without testing on Rspec or initially having rabl. Pretty much is a new app with rabl-rails from the beginning and checking the results on the browser.

Tonkpils commented 11 years ago

Scratch that, It seems that what was causing the error was this

node(:user) { partial('users/show', object: @user) }

Once I switched it to

node(:user) { partial('users/_show', object: @user) }

it worked. I'm going to write some rspec tests for it and see if I can replicate the issue

Tonkpils commented 11 years ago

So I was not able to reproduce the issue with the small app. I'll go back to big app and try to debug and see what the issue was.

freemanoid commented 11 years ago

I've got the same: https://github.com/ccocchi/rabl-rails/pull/15#issuecomment-13240843