我有模型Price
具有以下範圍和屬性。水豚和Rspec沒有範圍
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
這沒有做到。另外我使用Devise,如果它有助於使用'current_user'。我相信你不得不使用當前用戶的權利,但我正在環顧四周,無法找到任何相關信息。 – LearningRoR
你可以顯示「make_user_and_login」方法嗎? – railscard
我編輯了我的問題向你展示。 – LearningRoR