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