Rails 3.0.1 and rspec-rails assign issue: render doesn't have instance var

If you find the exception “undefined method ‘model_name’ for
NilClass:Class” in view specs with Rails 3.0.1 and rspec-rails2, I
believe it’s a Rails issue.

I receive the exception after calling render in my view spec, which
indicates the @player instance var does not exist in the view. To
troubleshoot, I added the following lines to my spec just below the
assign:

assign(:player, mock_model("Player").as_new_record)
puts "#{_encapsulated_assigns[:player]}"
puts "player=#{@player.inspect}"

Yielding:

Player_#<RSpec::Core::ExampleGroup::Nested_1:0x102e353e8>
player=nil

The Instance var is not there, but rspec-rails has it in the hash used
by ‘assign’.

I switched to Rails 3.0.5 and the issue went away. After going back to
3.0.1, it reoccured. There is some information regarding an assigns
issue with Rails 3.0.0 in the RSpec source code.

I’m posting this here because I couldn’t find anyone else having this
issue, so I’m simply sharing incase someone else does, to save them
time.

As a workaround, I’m skipping mock_model and creating a real AR for now.
I’ve not tried other Rails versions after 3.0.1 besides 3.0.5

require ‘spec_helper’

class Player < ActiveRecord::Base
end

describe “players/new.html.erb” do
it “displays the signup form” do
@player = Player.new # work around

puts "#{_encapsulated_assigns[:player]}"
puts "player=#{@player.inspect}"

render
rendered.should contain("Hello")

end
end

Brian

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs