unit-testing - mongoid、rspec 和分配(:people) issue

标签 unit-testing ruby-on-rails-3 rspec mongoid rspec2

我正在尝试为人员 Controller 编写我的第一个规范
如有必要,使用 mongoid (2.0.1)、rspec (2.5.0)、mongoid-rspec (1.4.2) 和制造 (0.9.5)。

(备注:模拟的 Organization 模型继承自 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

我收到了 以下错误消息 当我运行这个规范时:
    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)>'

由于inherited_resources(1.2.2),我的 Controller 是DRY的,并且可以在开发模式下正常工作。
class PeopleController < InheritedResources::Base
  actions :index
end

任何想法我做错了什么Mongoid::Criteria目的?

提前致谢

最佳答案

我有同样的问题,但捆绑后 mongoid-rspec问题消失了!

关于unit-testing - mongoid、rspec 和分配(:people) issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949722/

相关文章:

ruby-on-rails - 使用 rspec 通过正则表达式检查标签的属性

java - 异常 : mockito wanted but not invoked, 实际上与此模拟的交互为零

android - 如何用 Powermockito 模拟最后一个类?

ruby-on-rails - 在具有多个(太多?) ruby 的机器上为 RVM 设置全局默认 Ruby

mysql - 从父应用程序设置应用程序

ruby-on-rails - 如何为邮件拦截器编写 RSpec?

Android 应用程序单元测试

unit-testing - TDD:您为单元测试公开了哪些方法?

ruby-on-rails - 重定向到另一个 Controller 中的 SHOW 操作

ruby-on-rails - 失败/错误:引发 ActionController::RoutingError, "No route matches [#{env[' REQUEST_METHOD']}] #{env ['PATH_INFO' ].inspect}"