简体   繁体   English

另一个Location.count”没有变化1

[英]Another Location.count" didn't change by 1

Location is basically an address with other information fields. 位置基本上是带有其他信息字段的地址。 This is my first app and I followed Hartl and others in building it, but ignored failing tests that I couldn't fix at the time. 这是我的第一个应用程序,我跟随Hartl和其他人来构建它,但是忽略了当时无法修复的失败测试。 Now I'm trying to rectify that. 现在,我正在尝试纠正此问题。 I've looked at all the postings I found with this problem, but still can't figure it out (discussed at end). 我查看了所有发现的与此问题相关的帖子,但仍然无法弄清楚(最后讨论)。 The app works, I can create new locations, so I think the error is with the test. 该应用程序可以运行,我可以创建新位置,因此我认为测试出错。

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>'

Edited location_controller_test.rb (The location controller test has 8 tests, this is the one that fails): require 'test_helper' 编辑的location_controller_test.rb (位置控制器测试有8个测试,这是失败的测试):require'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, only: [:show, :edit, :update, :destroy] locations_controller.rb:类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 : 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

The model, location.rb : 模型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

If I turn the debugger on in the test, @location is 如果我在测试中打开调试器,则@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"

I'm not sure what to expect for this. 我不确定会发生什么。

One posting that seemed relevant "User.count" didn't change by 1 - Rails had incomplete yml—I've triple checked mine, but maybe still missing something. 一篇似乎与“ User.count”相关的帖子并未更改1-Rails的 yml不完整-我对我的进行了三遍检查,但也许仍然缺少一些内容。

@ArtOfCode. @ArtOfCode。 Creating in console (I think this is what you're asking). 在控制台中创建(我想这就是您要的内容)。 And since id is nil and it doesn't appear in the database, you may be on the right track: 而且由于idnil并且它没有出现在数据库中,因此您可能走在正确的轨道上:

    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 ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) 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"
    }  

The application is incomplete, but you can see a location here . 该应用程序不完整,但是您可以在此处看到一个位置。 Not currently allowing sign-ups, so obviously create can't be used. 当前不允许注册,因此显然无法使用create。 The addresses are more than 100 years old and the geo coordinates may not be generated. 地址已有100多年的历史,可能不会生成地理坐标。 geom is created later in PostGIS. geom稍后在PostGIS中创建。

I imagine there is a simple solution, but it alludes me. 我想有一个简单的解决方案,但它暗示了我。 gem 'rails' , '4.2.7' and ruby '2.3.1' gem 'rails' , '4.2.7'ruby '2.3.1'

Fixtures create database entries automatically. 夹具自动创建数据库条目。 Your location fixture one exists in the database. 您的定位装置one存在于数据库中。

So when you try a post to create a new location and you specify... 因此,当您尝试发布信息以创建新位置并指定...

  post :create, location: { address: @location.address,

You are trying to create a location with an address that already exists, but 您正在尝试使用已存在的地址创建位置,但是

validates :address, length: { minimum: 5, maximum: 50 }, uniqueness: true

...specifies that the address must be unique, so the record you're attempting to post is not valid because it has the same address as an existing record. ...指定地址必须是唯一的,因此您要发布的记录无效,因为它的地址与现有记录的地址相同。

Simply change the address in your post :create call 只需更改post :create的地址即可post :create call

  post :create, location: { address: "1 A New Address",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM