2016-05-19 3 views
1

に渡すために参加し、私はリードのモデルを持っているとコントローラ#インデックスはRSpecの

# A Lead is a type of User that might be interested in using our service. 
class Lead < User 
    validates :first_name, presence: true 
    has_many :notes, dependent: :destroy 

    def self.search(search) 
    ... 
    end 
end 

を取得鉛は、すべてのユーザーが:address, :city, :stateのようなデータを含むものLocationを、持っているユーザー

class User < ActiveRecord::Base 
    has_one :location, dependent: :destroy 
    accepts_nested_attributes_for :location 

    delegate :address, :city, :state, :zip_code, 
      :full_location, :latitude, :longitude, 
      to: :location, allow_nil: true 
end 

から継承します

私はこのコントローラを持っています

class LeadsController < ApplicationController 
    helper_method :sort_column 

    def index 
    @leads = Lead.search(params[:search]) 
       .joins(:location) 
       .order("#{sort_column} #{sort_direction}") 
       .page(params[:page]) 
       .per(8) 
    end 
end 

私はこのテスト

describe LeadsController, type: :controller do 
    before do 
    @lead = create :lead 
    end 

    describe 'GET #index' do 
    it 'populates an array of leads' do 
     get :index 
     assigns(:leads).should eq [@lead] 
    end 
end 

を失敗し、@leadsが空であると言うスペックを持っています。私はすべてがdevelopmentで正常に動作LeadsController

にライン.joins(:location)を削除すると

スペックを渡します。アプリはすべての正しいデータをプルアップして表示することができます。

何らかの理由により、test環境の.joinsは、@leadsを空にします。

私はその行が必要です。私はリード(ユーザー)をzip_codeでソートできるようにする必要があります。 zip_codeはLocationオブジェクトに格納されています。

私の質問は:郵便番号のソート可能性を維持しながら仕様をパスするにはどうすればよいですか? test環境ではどうなりますか?

ありがとうございます!

+0

'.joins(:location)'を '.search'の前に置いてみましたか? – mmichael

+0

@mmichael私は持っています。それでもなお失敗します。 –

+0

あなたの工場はどのように見えますか? –

答えて

1

LocationなしでLeadのテストが設定されているようです。 joinsINNER JOINを実行するので、関連付けられた場所を持つすべてのリードをプルする予定です。場所のないリードはではなく、が返されます。

User.joins(:posts) 
=> SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" 

あなたが行うことができます:

​​

と、あなたのテストに合格する必要があり

これはRailsのdocsからの素敵な例があります。

+1

あなたは正しいですあなたの答えを受け入れました。そのことを忘れてしまった。また、場所のないリードは返されないことも学びました。ありがとうございました! –

関連する問題