我有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
。为什么以及如何解决这个问题?
谢谢你。
支持/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