我正在使用country_selector gem制作注册表单。 表单工作得非常好,但我的测试错误:
ERROR["test_should_get_new", UsersControllerTest, 1.27725670899963]
test_should_get_new#UsersControllerTest (1.28s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `country_select' for #<ActionView::Helpers::FormBuilder:0x00000009488a88>
app/views/users/new.html.erb:49:in `block in _app_views_users_new_html_erb___2472929520155022240_77816800'
app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2472929520155022240_77816800'
test/controllers/users_controller_test.rb:5:in `block in <class:UsersControllerTest>'
ERROR["test_invalid_signup_information", UsersSignupTest, 1.3120288120117038]
test_invalid_signup_information#UsersSignupTest (1.31s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `country_select' for #<ActionView::Helpers::FormBuilder:0x00000009688220>
app/views/users/new.html.erb:49:in `block in _app_views_users_new_html_erb___2472929520155022240_78887340'
app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2472929520155022240_78887340'
test/integration/users_signup_test.rb:6:in `block in <class:UsersSignupTest>'
ERROR["test_valid_signup_information", UsersSignupTest, 1.3455659089959227]
test_valid_signup_information#UsersSignupTest (1.35s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `country_select' for #<ActionView::Helpers::FormBuilder:0x0000000542f798>
app/views/users/new.html.erb:49:in `block in _app_views_users_new_html_erb___2472929520155022240_44232140'
app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2472929520155022240_44232140'
test/integration/users_signup_test.rb:17:in `block in <class:UsersSignupTest>'
我不确定为什么在测试环境中似乎无法访问此gem。这是我的Gemfile,其中country_select位于主要宝石列表的底部:
source 'https://rubygems.org'
gem 'rails', '5.0.0.1'
gem 'bcrypt', '3.1.11'
gem 'faker', '1.6.6'
gem 'carrierwave', '0.11.2'
gem 'mini_magick', '4.5.1'
gem 'fog', '1.38.0'
gem 'will_paginate', '3.1.0'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass', '3.3.6'
gem 'puma', '3.4.0'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.0.0'
gem 'coffee-rails', '4.2.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.4.1'
gem 'country_select', '3.0.0'
group :development, :test do
gem 'sqlite3', '1.3.12'
gem 'byebug', '9.0.0', platform: :mri
end
group :development do
gem 'web-console', '3.1.1'
gem 'listen', '3.0.8'
gem 'spring', '1.7.2'
gem 'spring-watcher-listen', '2.0.0'
end
group :test do
gem 'rails-controller-testing', '0.1.1'
gem 'minitest-reporters', '1.1.9'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
end
group :production do
gem 'pg', '0.18.4'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
有错误的测试:
users_controller_tests.rb:
test "should get new" do
get signup_path
assert_response :success
end
users_signup_test.rb:
test "invalid signup information" do
get signup_path
assert_no_difference 'User.count' do
post users_path, params: { user: { first_name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar" } }
end
assert_template 'users/new'
end
test "valid signup information" do
get signup_path
assert_difference 'User.count', 1 do
post users_path, params: { user: { first_name: "Natalie", last_name: "Klein", email: "klien@example.com", city: "Melbourne", post_code: "3000", country: "Australia", self_summary: "Blah blah whatever whatever", password: "foobar1", password_confirmation: "foobar1" } }
end
follow_redirect!
assert_template 'users/show'
end
编辑:这是users / new.html.erb。
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: signup_path) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :city %>
<%= f.text_field :city, class: 'form-control' %>
<%= f.label :post_code %>
<%= f.text_field :post_code, class: 'form-control' %>
<%= f.label :country %>
<%= f.country_select :country, priority_countries: ["AU", "US", "GB", "CA", "NZ"], class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>