我一直在尝试做RSpec Book,黄瓜模拟浏览器部分。问题是它不断为任何对象抛出未定义的方法,这意味着它无法测试应用程序。
测试
When /^I create a movie Caddyshack in the Comedy genre$/ do
visit movies_path
click_link "Add Movie"
click_button "Save"
end
问题是它不会使用视图呈现表单,为ActiveRecord对象抛出未定义的错误。
视图
<%= 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 %>
控制器
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
模特
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
我的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
最后,水豚不断告诉我的错误
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'
我的功能/支持/ 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
然而,定义了name方法,并且我的浏览器中的页面渲染效果很好。有什么可能出错的想法吗?
答案 0 :(得分:1)
正如RobertH在评论中所述,我没有设置测试数据库。我需要做的只是运行
rake db:test:prepare