另一个 Location.count" 没变 1
Another Location.count" didn't change by 1
位置基本上是一个地址加上其他信息字段。这是我的第一个应用程序,我跟随 Hartl 和其他人构建它,但忽略了我当时无法修复的失败测试。现在我正在努力纠正这一点。我已经查看了我发现的所有关于这个问题的帖子,但仍然无法弄清楚(最后讨论)。该应用程序有效,我可以创建新位置,所以我认为错误与测试有关。
FAIL["test_should_create_location", LocationsControllerTest, 2017-02-28 12:02:08 -0800]
test_should_create_location#LocationsControllerTest (1488312128.31s)
"Location.count" didn't change by 1.
Expected: 4
Actual: 3
test/controllers/locations_controller_test.rb:21:in `block in <class:LocationsControllerTest>'
已编辑location_controller_test.rb
(位置控制器测试有 8 项测试,这是失败的一项):
需要 'test_helper'
class LocationsControllerTest < ActionController::TestCase
setup do
@location = locations(:one)
end
test "should create location" do
assert_difference('Location.count') do
post :create, location: { address: @location.address,
city: @location.city,
state: @location.state,
longitude: @location.longitude,
latitude: @location.latitude,
geom: @location.geom,
coords_not_locked: @location.coords_not_locked,
geocoded_with: @location.geocoded_with,
extant: @location.extant,
current_description: @location.current_description,
source: @location.source,
ref_link: @location.ref_link,
notes: @location.notes }
# debugger
end
assert_redirected_to location_path(assigns(:location))
end
locations_controller.rb:
class LocationsController < ApplicationController
helper_method :sort_column, :sort_direction
before_action :set_location, 仅: [:show, :edit, :update, :destroy]
def index
@locations = Location.all
# For sortable columns
@locations = Location.order(sort_column + " " + sort_direction)
end
def show
end
def new
@location= Location.new({:address=> "Use W E etc and St without PERIODS"})
repopulateResidResto()
end
def edit
end
def create
# Instantiate a new object using form parameters (notes here from Lynda>Skoglund)
@location = Location.new(location_params)
# Save the object
respond_to do |format|
if @location.save
# If save succeeds, redirect to the index action
format.html { redirect_to @location, notice: 'Address was successfully created.' }
format.json { render :show, status: :created, location: @location}
else
# If save fails, redisplay the form with information user filled in
format.html { render :new }
format.json { render json: @location.errors, status: :unprocessable_entity }
end
end
repopulateResidResto()
end # end create
def update
respond_to do |format|
if @location.update(location_params)
# If update succeeds, redirect to the show page
format.html { redirect_to @location, notice: 'Address was successfully updated.' }
format.json { render :show, status: :ok, location: @location }
else
# If update fails, redisplay the edit form for fixing
format.html { render :edit }
format.json { render json: @location.errors, status: :unprocessable_entity }
end
end
repopulateResidResto()
end # End update
def destroy
location = @location.destroy
respond_to do |format|
format.html { redirect_to locations_url, notice: "Location '#{location}' was successfully destroyed." }
format.json { head :no_content }
end
repopulateResidResto()
end
private
# Use callbacks to share common setup or constraints between actions.
def set_location
@location = Location.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def location_params
params.require(:location).permit(:address, :city, :state, :longitude, :latitude, :geom, :coords_not_locked, :geocoded_with, :extant, :current_description, :source, :ref_link, :notes)
end
def sort_column
Location.column_names.include?(params[:sort]) ? params[:sort] : "address"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end
locations.yml
:
one:
address: 1 Address1
city: Los Angeles
state: California
longitude: 99.99
latitude: 99.99
extant: false
current_description: MyString2
notes: Notes1
source: Source1
geocoded_with: geocoded_with_1
coords_not_locked: false
ref_link: ref_link_1
geom: 0101000020E61000008B187618938F5DC0C2189128B4064140
two:
address: 2 Address2
city: Los Angeles
state: California
longitude: 9.99
latitude: 9.99
extant: true
current_description: MyString
notes: MyString
source: MyString
geocoded_with: MyString
coords_not_locked: true
ref_link: MyString
geom: 0101000020E61000007B4963B48E8F5DC0467C2766BD064140
three:
address: 3 Address3
city: Los Angeles
state: California
longitude: 9.99
latitude: 9.99
extant: true
current_description: MyString
notes: MyString3
source: MyString3
geocoded_with: MyString3
coords_not_locked: true
ref_link: MyString3
geom: 0101000020E61000007B4963B48E8F5DC0467C2766BD064140
型号,location.rb
:
class Location < ActiveRecord::Base
has_many :years, dependent: :destroy
has_many :people, through: :years
has_many :resto_resid_lines
def longlat
"#{longitude} #{latitude}"
end
def notes_plus_geocode
if notes == ""
"#{geocoded_with}"
else
"#{notes} #{geocoded_with}"
end
end
def full_address
full_address = "#{address}, #{city}, #{state}"
end
# For next and previous in show.
def next
Location.where(["id > ?", id]).first
end
def previous
Location.where(["id < ?", id]).last
end
geocoded_by :full_address
after_validation :geocode, :if => :coords_not_locked?
validates :address, length: { minimum: 5, maximum: 50 }, uniqueness: true
end
test_helper.rb
require 'simplecov'
SimpleCov.start
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
fixtures :all
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Logs in a test user.
def log_in_as(user, options = {})
password = options[:password] || 'password'
remember_me = options[:remember_me] || '1'
if integration_test?
post login_path, session: { email: user.email,
password: password,
remember_me: remember_me }
else
session[:user_id] = user.id
end
end
private
# Returns true inside an integration test.
def integration_test?
defined?(post_via_redirect)
end
end
如果我在测试中打开调试器,@location
是
(byebug) pp @location
#<Location:0x007ff26ffa1ba8
id: 980190962,
address: "MyString21",
city: "MyString23",
state: "MyString25",
longitude: #<BigDecimal:7ff26ff96b40,'0.9999E2',18(27)>,
latitude: #<BigDecimal:7ff26ff96a50,'0.9999E2',18(27)>,
extant: false,
current_description: "MyString2",
notes: "MyString24",
created_at: Sun, 05 Mar 2017 18:46:12 UTC +00:00,
updated_at: Sun, 05 Mar 2017 18:46:12 UTC +00:00,
geom: "0101000020E61000008B187618938F5DC0C2189128B4064140",
source: "MyString",
geocoded_with: "MyString",
coords_not_locked: false,
ref_link: "MyString">
#<Location id: 980190962, address: "MyString21", city: "MyString23", state: "MyString25", longitude: #<BigDecimal:7ff26ff96b40,'0.9999E2',18(27)>, latitude: #<BigDecimal:7ff26ff96a50,'0.9999E2',18(27)>, extant: false, current_description: "MyString2", notes: "MyString24", created_at: "2017-03-05 18:46:12", updated_at: "2017-03-05 18:46:12", geom: "0101000020E61000008B187618938F5DC0C2189128B4064140", source: "MyString", geocoded_with: "MyString", coords_not_locked: false, ref_link: "MyString"
我不确定会发生什么。
一个看起来相关的帖子 的 yml 不完整——我已经三重检查了我的,但可能仍然遗漏了一些东西。
@ArtOfCode。在控制台中创建(我想这就是你要问的)。由于 id
是 nil
并且它没有出现在数据库中,您可能是在正确的轨道上:
irb(main):004:0> location = Location.create( address: "1 Address1", city: "Los Angeles", state: "California", longitude: 99.99, latitude: 99.99, extant: false, current_description: "MyString2", notes: "MyString24", source: "MyString", geocoded_with: "MyString", coords_not_locked: false, ref_link: "MyString", geom: "0101000020E61000008B187618938F5DC0C2189128B4064140")
(0.2ms) BEGIN
Location Exists (0.4ms) SELECT 1 AS one FROM "locations" WHERE "locations"."address" = '1 Address1' LIMIT 1
SQL (2.5ms) INSERT INTO "locations" ("address", "state", "longitude", "latitude", "extant", "current_description", "notes", "source", "geocoded_with", "coords_not_locked", "ref_link", "geom", "created_at", "updated_at") VALUES (, , , , , , , , , , , , , ) RETURNING "id" [["address", "1 Address1"], ["state", "California"], ["longitude", "99.99"], ["latitude", "99.99"], ["extant", "f"], ["current_description", "MyString2"], ["notes", "MyString24"], ["source", "MyString"], ["geocoded_with", "MyString"], ["coords_not_locked", "f"], ["ref_link", "MyString"], ["geom", "0101000020E61000008B187618938F5DC0C2189128B4064140"], ["created_at", "2017-03-05 20:00:28.246188"], ["updated_at", "2017-03-05 20:00:28.246188"]]
(2.1ms) COMMIT
#<Location:0x007fe851a9bac8> {
:id => 177,
:address => "1 Address1",
:city => "Los Angeles",
:state => "California",
:longitude => 99.99,
:latitude => 99.99,
:extant => false,
:current_description => "MyString2",
:notes => "MyString24",
:created_at => Sun, 05 Mar 2017 20:00:28 UTC +00:00,
:updated_at => Sun, 05 Mar 2017 20:00:28 UTC +00:00,
:geom => "0101000020E61000008B187618938F5DC0C2189128B4064140",
:source => "MyString",
:geocoded_with => "MyString",
:coords_not_locked => false,
:ref_link => "MyString"
}
申请不完整,但可以看到一个位置here。目前不允许注册,所以显然不能使用 create。地址已有 100 多年历史,可能无法生成地理坐标。 geom
稍后在 PostGIS 中创建。
我想有一个简单的解决方案,但它暗示了我。 gem 'rails' , '4.2.7'
和 ruby '2.3.1'
装置自动创建数据库条目。您的定位装置 one
存在于数据库中。
因此,当您尝试 post 创建新位置并指定...
post :create, location: { address: @location.address,
您正在尝试使用已存在的地址创建位置,但是
validates :address, length: { minimum: 5, maximum: 50 }, uniqueness: true
...指定地址必须是唯一的,因此您尝试 post 的记录无效,因为它与现有记录具有相同的地址。
只需更改 post :create
电话中的地址
post :create, location: { address: "1 A New Address",
位置基本上是一个地址加上其他信息字段。这是我的第一个应用程序,我跟随 Hartl 和其他人构建它,但忽略了我当时无法修复的失败测试。现在我正在努力纠正这一点。我已经查看了我发现的所有关于这个问题的帖子,但仍然无法弄清楚(最后讨论)。该应用程序有效,我可以创建新位置,所以我认为错误与测试有关。
FAIL["test_should_create_location", LocationsControllerTest, 2017-02-28 12:02:08 -0800]
test_should_create_location#LocationsControllerTest (1488312128.31s)
"Location.count" didn't change by 1.
Expected: 4
Actual: 3
test/controllers/locations_controller_test.rb:21:in `block in <class:LocationsControllerTest>'
已编辑location_controller_test.rb
(位置控制器测试有 8 项测试,这是失败的一项):
需要 'test_helper'
class LocationsControllerTest < ActionController::TestCase
setup do
@location = locations(:one)
end
test "should create location" do
assert_difference('Location.count') do
post :create, location: { address: @location.address,
city: @location.city,
state: @location.state,
longitude: @location.longitude,
latitude: @location.latitude,
geom: @location.geom,
coords_not_locked: @location.coords_not_locked,
geocoded_with: @location.geocoded_with,
extant: @location.extant,
current_description: @location.current_description,
source: @location.source,
ref_link: @location.ref_link,
notes: @location.notes }
# debugger
end
assert_redirected_to location_path(assigns(:location))
end
locations_controller.rb: class LocationsController < ApplicationController helper_method :sort_column, :sort_direction before_action :set_location, 仅: [:show, :edit, :update, :destroy]
def index
@locations = Location.all
# For sortable columns
@locations = Location.order(sort_column + " " + sort_direction)
end
def show
end
def new
@location= Location.new({:address=> "Use W E etc and St without PERIODS"})
repopulateResidResto()
end
def edit
end
def create
# Instantiate a new object using form parameters (notes here from Lynda>Skoglund)
@location = Location.new(location_params)
# Save the object
respond_to do |format|
if @location.save
# If save succeeds, redirect to the index action
format.html { redirect_to @location, notice: 'Address was successfully created.' }
format.json { render :show, status: :created, location: @location}
else
# If save fails, redisplay the form with information user filled in
format.html { render :new }
format.json { render json: @location.errors, status: :unprocessable_entity }
end
end
repopulateResidResto()
end # end create
def update
respond_to do |format|
if @location.update(location_params)
# If update succeeds, redirect to the show page
format.html { redirect_to @location, notice: 'Address was successfully updated.' }
format.json { render :show, status: :ok, location: @location }
else
# If update fails, redisplay the edit form for fixing
format.html { render :edit }
format.json { render json: @location.errors, status: :unprocessable_entity }
end
end
repopulateResidResto()
end # End update
def destroy
location = @location.destroy
respond_to do |format|
format.html { redirect_to locations_url, notice: "Location '#{location}' was successfully destroyed." }
format.json { head :no_content }
end
repopulateResidResto()
end
private
# Use callbacks to share common setup or constraints between actions.
def set_location
@location = Location.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def location_params
params.require(:location).permit(:address, :city, :state, :longitude, :latitude, :geom, :coords_not_locked, :geocoded_with, :extant, :current_description, :source, :ref_link, :notes)
end
def sort_column
Location.column_names.include?(params[:sort]) ? params[:sort] : "address"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end
locations.yml
:
one:
address: 1 Address1
city: Los Angeles
state: California
longitude: 99.99
latitude: 99.99
extant: false
current_description: MyString2
notes: Notes1
source: Source1
geocoded_with: geocoded_with_1
coords_not_locked: false
ref_link: ref_link_1
geom: 0101000020E61000008B187618938F5DC0C2189128B4064140
two:
address: 2 Address2
city: Los Angeles
state: California
longitude: 9.99
latitude: 9.99
extant: true
current_description: MyString
notes: MyString
source: MyString
geocoded_with: MyString
coords_not_locked: true
ref_link: MyString
geom: 0101000020E61000007B4963B48E8F5DC0467C2766BD064140
three:
address: 3 Address3
city: Los Angeles
state: California
longitude: 9.99
latitude: 9.99
extant: true
current_description: MyString
notes: MyString3
source: MyString3
geocoded_with: MyString3
coords_not_locked: true
ref_link: MyString3
geom: 0101000020E61000007B4963B48E8F5DC0467C2766BD064140
型号,location.rb
:
class Location < ActiveRecord::Base
has_many :years, dependent: :destroy
has_many :people, through: :years
has_many :resto_resid_lines
def longlat
"#{longitude} #{latitude}"
end
def notes_plus_geocode
if notes == ""
"#{geocoded_with}"
else
"#{notes} #{geocoded_with}"
end
end
def full_address
full_address = "#{address}, #{city}, #{state}"
end
# For next and previous in show.
def next
Location.where(["id > ?", id]).first
end
def previous
Location.where(["id < ?", id]).last
end
geocoded_by :full_address
after_validation :geocode, :if => :coords_not_locked?
validates :address, length: { minimum: 5, maximum: 50 }, uniqueness: true
end
test_helper.rb
require 'simplecov'
SimpleCov.start
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
fixtures :all
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Logs in a test user.
def log_in_as(user, options = {})
password = options[:password] || 'password'
remember_me = options[:remember_me] || '1'
if integration_test?
post login_path, session: { email: user.email,
password: password,
remember_me: remember_me }
else
session[:user_id] = user.id
end
end
private
# Returns true inside an integration test.
def integration_test?
defined?(post_via_redirect)
end
end
如果我在测试中打开调试器,@location
是
(byebug) pp @location
#<Location:0x007ff26ffa1ba8
id: 980190962,
address: "MyString21",
city: "MyString23",
state: "MyString25",
longitude: #<BigDecimal:7ff26ff96b40,'0.9999E2',18(27)>,
latitude: #<BigDecimal:7ff26ff96a50,'0.9999E2',18(27)>,
extant: false,
current_description: "MyString2",
notes: "MyString24",
created_at: Sun, 05 Mar 2017 18:46:12 UTC +00:00,
updated_at: Sun, 05 Mar 2017 18:46:12 UTC +00:00,
geom: "0101000020E61000008B187618938F5DC0C2189128B4064140",
source: "MyString",
geocoded_with: "MyString",
coords_not_locked: false,
ref_link: "MyString">
#<Location id: 980190962, address: "MyString21", city: "MyString23", state: "MyString25", longitude: #<BigDecimal:7ff26ff96b40,'0.9999E2',18(27)>, latitude: #<BigDecimal:7ff26ff96a50,'0.9999E2',18(27)>, extant: false, current_description: "MyString2", notes: "MyString24", created_at: "2017-03-05 18:46:12", updated_at: "2017-03-05 18:46:12", geom: "0101000020E61000008B187618938F5DC0C2189128B4064140", source: "MyString", geocoded_with: "MyString", coords_not_locked: false, ref_link: "MyString"
我不确定会发生什么。
一个看起来相关的帖子
@ArtOfCode。在控制台中创建(我想这就是你要问的)。由于 id
是 nil
并且它没有出现在数据库中,您可能是在正确的轨道上:
irb(main):004:0> location = Location.create( address: "1 Address1", city: "Los Angeles", state: "California", longitude: 99.99, latitude: 99.99, extant: false, current_description: "MyString2", notes: "MyString24", source: "MyString", geocoded_with: "MyString", coords_not_locked: false, ref_link: "MyString", geom: "0101000020E61000008B187618938F5DC0C2189128B4064140")
(0.2ms) BEGIN
Location Exists (0.4ms) SELECT 1 AS one FROM "locations" WHERE "locations"."address" = '1 Address1' LIMIT 1
SQL (2.5ms) INSERT INTO "locations" ("address", "state", "longitude", "latitude", "extant", "current_description", "notes", "source", "geocoded_with", "coords_not_locked", "ref_link", "geom", "created_at", "updated_at") VALUES (, , , , , , , , , , , , , ) RETURNING "id" [["address", "1 Address1"], ["state", "California"], ["longitude", "99.99"], ["latitude", "99.99"], ["extant", "f"], ["current_description", "MyString2"], ["notes", "MyString24"], ["source", "MyString"], ["geocoded_with", "MyString"], ["coords_not_locked", "f"], ["ref_link", "MyString"], ["geom", "0101000020E61000008B187618938F5DC0C2189128B4064140"], ["created_at", "2017-03-05 20:00:28.246188"], ["updated_at", "2017-03-05 20:00:28.246188"]]
(2.1ms) COMMIT
#<Location:0x007fe851a9bac8> {
:id => 177,
:address => "1 Address1",
:city => "Los Angeles",
:state => "California",
:longitude => 99.99,
:latitude => 99.99,
:extant => false,
:current_description => "MyString2",
:notes => "MyString24",
:created_at => Sun, 05 Mar 2017 20:00:28 UTC +00:00,
:updated_at => Sun, 05 Mar 2017 20:00:28 UTC +00:00,
:geom => "0101000020E61000008B187618938F5DC0C2189128B4064140",
:source => "MyString",
:geocoded_with => "MyString",
:coords_not_locked => false,
:ref_link => "MyString"
}
申请不完整,但可以看到一个位置here。目前不允许注册,所以显然不能使用 create。地址已有 100 多年历史,可能无法生成地理坐标。 geom
稍后在 PostGIS 中创建。
我想有一个简单的解决方案,但它暗示了我。 gem 'rails' , '4.2.7'
和 ruby '2.3.1'
装置自动创建数据库条目。您的定位装置 one
存在于数据库中。
因此,当您尝试 post 创建新位置并指定...
post :create, location: { address: @location.address,
您正在尝试使用已存在的地址创建位置,但是
validates :address, length: { minimum: 5, maximum: 50 }, uniqueness: true
...指定地址必须是唯一的,因此您尝试 post 的记录无效,因为它与现有记录具有相同的地址。
只需更改 post :create
电话中的地址
post :create, location: { address: "1 A New Address",