我有一个非常简单的管理面板,其中显示了总用户数,本周新用户数以及本月新用户数。我想在我的控制器规范中为它编写一个测试。目前我正在努力让用户总数达到目标。编码新手。谢谢!
我收到了这个错误:
1) Admin::DashboardController metrics shows correct metrics for # of users
Failure/Error: users = FactoryGirl.create_list([@user_1, @user_2, @user_3])
ArgumentError:
wrong number of arguments (1 for 2+)
dashboard_controller_spec.rb:
describe 'metrics' do
before :each do
@user_1 = FactoryGirl.create(:user, created_at: Time.now)
@user_2 = FactoryGirl.create(:user, created_at: Time.now - 1.week)
@user_3 = FactoryGirl.create(:user, created_at: Time.now - 4.week)
end
it 'shows correct metrics for # of users' do
users = FactoryGirl.create_list([@user_1, @user_2, @user_3])
expect(assigns(:metrics[:users][:total])).to eq(users)
end
it 'shows correct number of new users this week' do
end
it 'shows correct number of new users this month' do
end
end
dashboard_controller.rb
class Admin::DashboardController < ApplicationController
before_action :authorize_user!
def index
@metrics = {
users: {
total: User.count,
this_week: User.where('created_at >= ?', 1.week.ago).count,
this_month: User.where('created_at >= ?', 4.weeks.ago).count
}
}
end
end
答案 0 :(得分:0)
我找到了答案:
describe 'metrics' do
before :each do
@user_1 = FactoryGirl.create(:user, created_at: Time.zone.now)
@user_2 = FactoryGirl.create(:user, created_at: Time.zone.now - 1.week)
@user_3 = FactoryGirl.create(:user, created_at: Time.zone.now - 4.weeks)
end
it 'shows correct metrics for # of users' do
expect do
users = FactoryGirl.create_list([@user_1, @user_2, @user_3])
assigns(:metrics).to eq(users)
end
end
it 'shows correct number of new users this week' do
expect do
users = FactoryGirl.create_list([@user_1, @user_2, @user_3])
assigns(:metrics[users][last_week]).to eq(users)
end
end
it 'shows correct number of new users this month' do
expect do
users = FactoryGirl.create_list([@user_1, @user_2, @user_3])
assigns(:metrics[users][last_month]).to eq(users)
end
end
端