mongoid,rspec和指定(:人)问题

tma*_*ier 6 unit-testing rspec mongoid rspec2 ruby-on-rails-3

我正在尝试使用mongoid(2.0.1),rspec(2.5.0),mongoid-rspec(1.4.2)和制作(0.9.5)为People Controller编写我的第一个规范,如果有必要的话.

(注释:组织模型模拟继承自Person模型)

describe PeopleController do
  describe "as logged in user" do
    before (:each) do
      @user = Fabricate(:user)
      sign_in @user
    end

    describe "GET 'index'" do
      def mock_person(stubs={})
        @mock_person ||= mock_model(Person, stubs).as_null_object
#        @mock_person ||= Fabricate.build(:organization)       
      end

      it "should be successful" do
        get :index
        response.should be_success
      end

      it "assigns all people as @people" do
        Person.stub(:all) { [mock_person] }
        get :index
        assigns(:people).should eq(mock_person)
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

运行此规范时,我收到以下错误消息:

    1) PeopleController as logged in user GET 'index' assigns all people as @people
       Failure/Error: assigns(:people).should eq(mock_person)

         expected #<Person:0x811b8448 @name="Person_1001">
              got #<Mongoid::Criteria
           selector: {},
           options:  {},
           class:    Person,
           embedded: false>


         (compared using ==)

         Diff:
         @@ -1,2 +1,6 @@
         -#<Person:0x811b8448 @name="Person_1001">
         +#<Mongoid::Criteria
         +  selector: {},
         +  options:  {},
         +  class:    Person,
         +  embedded: false>
       # ./spec/controllers/people_controller_spec.rb:24:in `block (4 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

由于inherited_resources(1.2.2),我的控制器是DRY,并且它应该在开发模式下工作.

class PeopleController < InheritedResources::Base
  actions :index
end
Run Code Online (Sandbox Code Playgroud)

我在做什么错误的Mongoid::Criteria对象?

提前致谢

Ree*_*Law 0

不幸的是,我遇到了同样的问题,我能想到的唯一解决方案是将其放入控制器中:

def index
  @people = Person.all.to_a
end
Run Code Online (Sandbox Code Playgroud)

to_a方法会将对象转换Mongoid::Criteria为数组。这种方法对我有用,但我不知道你会如何使用inherited_resources。