2017-03-05 24 views
0

位置基本上是一個其他信息字段的地址,這是我的第一個應用程序,我跟着Hartl和其他人建立它,但忽略了我無法修復的失敗測試現在我試圖糾正這個問題,我查看了所有發現這個問題的帖子,但仍然無法弄清楚(最後討論)。該應用可以工作,我可以創建新的位置,所以我認爲錯誤是與測試另一個Location.count「沒有改變1

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: 類LocationsController <的ApplicationController 是helper_method:排序依據,:sort_direction before_action:set_location,只有:[:顯示,編輯,:更新:摧毀]

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" 

我不知道會發生什麼事情了這一點。

一個帖子,似乎相關"User.count" didn't change by 1 - Rails有不完整的yml-我三重檢查了我的,但也許還錯過了一些東西。

@ArtOfCode。在控制檯中創建(我認爲這是你問的)。而且,由於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" 
    } 

申請資料不完整,但你可以看到的位置here。目前不允許註冊,所以顯然創建不能使用。地址已超過100年,可能不會生成地理座標。稍後在PostGIS中創建geom

我想有一個簡單的解決方案,但它暗示了我。 gem 'rails' , '4.2.7'ruby '2.3.1'

+0

試試,1 DO ... end'看看會發生什麼? – ArtOfCode

+0

同樣的結果(之前我曾嘗試過,但現在是結果)。 'FAIL [ 「test_should_create_location」,LocationsControllerTest,2017年2月28日12時02分08秒-0800]'' #test_should_create_location LocationsControllerTest(1488312128.41s) 「Location.count」 沒有通過1 預期變化:4 實際:3 test/controllers/locations_controller_test.rb:21:在的塊中''感謝您的迴應。 – Greg

+0

您是否已驗證'create'調用確實*創建了記錄(即使用控制檯)? – ArtOfCode

回答

1

夾具自動創建數據庫條目。您的位置夾具one存在於數據庫中。

因此,當您嘗試創建新位置的帖子並指定...

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

您試圖創建已存在的地址的位置,但

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

...指定的地址必須是唯一的,所以你嘗試發佈戰績無效,因爲它具有與現有記錄相同的地址。

post :create通話只需更改地址使用`一個assert_difference 'Location.count'

+0

謝謝。那樣做了。我不太清楚夾具是否被使用過兩次。一次用於創建新條目以及現有條目。測試需要更多的學習。再次感謝。我一直在對此進行抨擊一週。但我對調試的瞭解比以前更多。 – Greg