3

Price次のスコープと属性を持つモデルがあります。

def self.total
  self.sum(:amount) + self.sum(:tax)
end

def self.today
  where(:date => Date.today)
end

def self.for_data
  where(:bought => true)
end

現在のユーザーの今日の合計量を取得するスコープチェーンがあります。

<p class="today">
  Today
   <span class="amount today">
     <%= number_to_currency(current_user.prices.for_data.today.total) %>
   </span>
</p>

これをテストするための仕様を作成します。

# user_pages_spec

describe 'Overview Page' do
  it 'shows spending' do
     make_user_and_login
     price = FactoryGirl.create(:price)
     click_link('Overview') 
     page.should have_selector('title', :text => 'Overview')
     within('p.today', :text => 'Today') do
       page.should have_content('$1.01')
     end
  end
end

これは私の価格ファクトリーです:

factory :price do
  amount '1.00'
  tax '0.01'
  bought true
  date Date.today
end

残念ながら、これはエラーを返します:

1) UserPages Account Settings Data Page shows spending
 Failure/Error: page.should have_content('$1.01')
 expected there to be content "$1.01" in "\n\t\t\tToday\n\t\t\t$0.00\n\t\t"

ビューに手動で配置すること$1.01はできますが、スコープに依存している場合は機能しません。を返すので、ファクトリまたはスコープを一般的に検出していないように見えます$0.00。なぜそしてどのようにこれが解決されるのですか?

ありがとうございました。


support / user_macros.rb

module UserMacros
  def make_user_and_login
    user = FactoryGirl.create(:user)
    visit new_user_session_path
    page.should have_selector('title', :text => 'Login')
    fill_in('Email',    :with => user.email)
    fill_in('Password', :with => user.password)
    click_button('Login')
    page.should have_selector('title', :text => 'Home')
  end
end
4

2 に答える 2

2

問題は、価格レコードがcurrent_userと関係がないことだと思います。したがって、この場合、current_userの合計は実際には0.00です。次のように価格ファクトリを変更することで、これを気に入ってもらえます。

factory :price do
  amount '1.00'
  tax '0.01'
  bought true
  date Date.today
  user { User.first || FactoryGirl.create(:user) }
end
于 2012-09-19T01:19:49.453 に答える
1

これは私が推測する統合仕様であるため、インタラクティブな手順 (ログイン) の前にシード手順 (Factory の作成) を実行する必要があります。

以下を実行して、ファクトリの作成中にユーザーの関連付けを処理できます。

user = FactoryGirl.create(:user)
price = FactoryGirl.create(:price, user: user)
于 2012-09-19T00:05:04.107 に答える