2012-12-12 63 views
3

我想使用RSpec和Mongoid和rails-api一起使用。我使用的寶石是rspec-railsmongoid-rspec。一切都只是這個小東西罰款:RSpec和Mongoid一起使用rails-api

# users_controller_spec.rb 
describe "GET index" do 
    it "assigns all users as @users" do 
    user = User.create! valid_attributes 
    get :index, {}, valid_session 
    assigns(:users).should eq([user]) 
    end 
end 

# users_controller.rb 
def index 
    @users = User.all 
    render json: @users 
end 

做這件事時,我只收到此錯誤信息:

Failures: 

    1) UsersController GET index assigns all users as @users 
    Failure/Error: assigns(:users).should eq([user]) 

     expected: [#<User _id: 50c8b84606027eb8aa000001, _type: nil, created_at: 2012-12-12 17:00:54 UTC, updated_at: 2012-12-12 17:00:54 UTC, name: "testuser", email: "[email protected]">] 
      got: #<Mongoid::Criteria 
     selector: {} 
     options: {} 
     class: User 
     embedded: false> 


     (compared using ==) 

     Diff: 
     @@ -1,2 +1,6 @@ 
     -[#<User _id: 50c8b84606027eb8aa000001, _type: nil, created_at: 2012-12-12 17:00:54 UTC, updated_at: 2012-12-12 17:00:54 UTC, name: "testuser", email: "[email protected]">] 
     +#<Mongoid::Criteria 
     + selector: {} 
     + options: {} 
     + class: User 
     + embedded: false> 

    # ./spec/controllers/users_controller_spec.rb:41:in `block (3 levels) in <top (required)>' 
+0

我得到相同。不知道爲什麼。 –

回答

1

@users = User.all是延遲加載,所以實際的對象是一個標準。

您可以對證的標準,或使用數組是這樣的:

assigns(:users).to_a.should eq([user]) 
相關問題