I’ve been trying to do the RSpec Book, the cucumber simulated browser part. The problem is that it keeps throwing undefined method for any object, meaning it can’t test the application.
The test
When /^I create a movie Caddyshack in the Comedy genre$/ do
visit movies_path
click_link "Add Movie"
click_button "Save"
end
The problem is that it won’t render the form with the view, throwing undefined error for ActiveRecord objects.
The view
<%= form_for(@movie) do |f| %>
<table>
<tr>
<th><%= f.label :name %></th>
<td><%= f.text_field :name, :id => "title" %></td>
</tr>
</table>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The controller
def index
@movies = Movie.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @movies }
end
end
def new
@movie = Movie.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @movie }
end
end
The Model
class Movie < ActiveRecord::Base
attr_accessible :showtime_date, :showtime_time, :name
has_many :genres
def showtime
"#{formatted_date} (#{formatted_time})"
end
def formatted_date
showtime_date.strftime("%B %d, %Y")
end
def formatted_time
format_string = showtime_time.min.zero? ? "%l%p" : "%l:%M%p"
showtime_time.strftime(format_string).strip.downcase
end
end
My Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'sqlite3'
gem 'awesome_print'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
group :development, :test do
gem "rspec-rails", ">= 2.0.0"
end
group :test do
gem 'database_cleaner'
gem "cucumber-rails", ">= 0.3.2"
end
Finally, the error that capybara keeps telling me
When I create a movie Caddyshack in the Comedy genre # features/step_definitions/genre_steps.rb:6
undefined method `name' for #<Movie:0x00000102e36590> (ActionView::Template::Error)
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:407:in `method_missing'
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:149:in `method_missing'
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/helpers/form_helper.rb:1161:in `value_before_type_cast'
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/helpers/form_helper.rb:1149:in `value_before_type_cast'
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/helpers/form_helper.rb:1043:in `block in to_input_field_tag'
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/helpers/form_helper.rb:1043:in `fetch'
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/helpers/form_helper.rb:1043:in `to_input_field_tag'
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/helpers/form_helper.rb:692:in `text_field'
/Users/Dono/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/helpers/form_helper.rb:1284:in `text_field'
./app/views/movies/_form.html.erb:5:in `block in _app_views_movies__form_html_erb___3270649907202408240_2157451180'
My features/support/env.rb
require 'cucumber/rails'
require 'rspec'
Capybara.default_selector = :css
ActionController::Base.allow_rescue = false
begin
DatabaseCleaner.strategy = :transaction
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Cucumber::Rails::Database.javascript_strategy = :truncation
Yet the name method is defined and the page renders fine in my browser. Any idea on what might be going wrong?
As stated by RobertH in the comments, I wasn’t setting the test database. All I needed to do was to run