我有一个非常简单的控制器操作,但我的 RSpec 测试失败了,我不明白为什么。我希望有人能帮我看看我在哪里犯了错误。
这些操作只是获取GroupMember
给定User
的记录group_member.status == "approved"
测试失败,说返回的是 nil 而不是我期望的 GroupMember 值。
我知道它有效,因为我正在查看我的开发环境中的视图。
# users_controller
def groups
@group_member = current_user.group_members.approved
end
# user.rb
has_many :group_members, :dependent => :destroy
# routes.rb
resources :users do
collection do
get 'groups'
end
end
# group_member.rb
belongs_to :user
scope :approved, where(:status=>"approved")
# users_controller_spec.rb
describe "GET #groups" do
before :each do
@user = FactoryGirl.create(:user)
current_user = sign_in @user
@group_member = FactoryGirl.create(:group_member, user_id: @user.id, status: "approved")
end
it "assigns group member correctly" do
get :groups
assigns[:group_member].should eq [@group_member]
end
end