Forgive a very nooooob question. I am trying to get my head around all of this.
Have a couple of simple models (weird spelling of tipe was in case
type was a reserved word ...). They were all generated with the
rspec_scaffold generator
class Category < ActiveRecord::Base
has_many :tipes
validates_uniqueness_of :name
validates_presence_of :name
end
class Tipe < ActiveRecord::Base
belongs_to :category
validates_uniqueness_of :name
validates_presence_of :name
end
I have a line in my tipes/index.html.erb
<%=h tipe.category.name %>
All works just fine in the development environment, but when I run
autospec, it fails with the the following error:
"ActionView::TemplateError in ''/tipes/index.html.erb renders a
list of
tipes'' undefined method `name'' for nil:NilClass"
The tipes/index.html.erb_spec.rb is the standard rspec_scaffold generated one :
require ''spec_helper''
describe "/tipes/index.html.erb" do
include TipesHelper
before(:each) do
assigns[:tipes] = [
stub_model(Tipe,
:name => "value for name",
:live => false,
:category_id => 1
),
stub_model(Tipe,
:name => "value for name",
:live => false,
:category_id => 1
)
]
end
it "renders a list of tipes" do
render
response.should have_tag("tr>td", "value for
name".to_s, 2)
response.should have_tag("tr>td", false.to_s, 2)
response.should have_tag("tr>td", 1.to_s, 2)
end
end
Clearly there is some problem with the association - it isn''t
understanding it. So my dumb nooob question is ... what do I do to get
this to work? If I leave the line in the index.html.erb as <%=h
tipe.category_id %> it is all sweetness and light. If I change it ot
<%=h tipe.category.name %> the world is a dark, cold place...
(PS - I don''t really understand the assigns[:tipe] thing either ...)
(this is the error trace
app/views/tipes/index.html.erb:14
app/views/tipes/index.html.erb:10:in `each''
app/views/tipes/index.html.erb:10
vendor/gems/rspec-rails-1.3.2/lib/spec/rails/extensions/action_view/base.rb:27:in
`render''
vendor/gems/rspec-rails-1.3.2/lib/spec/rails/example/view_example_group.rb:170:in
`__send__''
vendor/gems/rspec-rails-1.3.2/lib/spec/rails/example/view_example_group.rb:170:in
`render''
spec/views/tipes/index.html.erb_spec.rb:30
rspec (1.3.0) lib/spec/example/example_methods.rb:40:in
`instance_eval''
rspec (1.3.0) lib/spec/example/example_methods.rb:40:in `execute''
/usr/local/lib/ruby/1.8/timeout.rb:53:in `timeout''
rspec (1.3.0) lib/spec/example/example_methods.rb:37:in `execute''
rspec (1.3.0) lib/spec/example/example_group_methods.rb:214:in
`run_examples''
rspec (1.3.0) lib/spec/example/example_group_methods.rb:212:in
`each''
rspec (1.3.0) lib/spec/example/example_group_methods.rb:212:in
`run_examples''
rspec (1.3.0) lib/spec/example/example_group_methods.rb:103:in
`run''
rspec (1.3.0) lib/spec/runner/example_group_runner.rb:23:in `run''
rspec (1.3.0) lib/spec/runner/example_group_runner.rb:22:in `each''
rspec (1.3.0) lib/spec/runner/example_group_runner.rb:22:in `run''
rspec (1.3.0) lib/spec/runner/options.rb:152:in `run_examples''
rspec (1.3.0) lib/spec/runner/command_line.rb:9:in `run''
rspec (1.3.0) bin/spec:5
)
Any help v much appreciated ...
Ben