开发者

mongoid, rspec and assigns(:people) issue

开发者 https://www.devze.com 2023-03-05 00:55 出处:网络
I\'m trying to write my first specs for the People Controller Using mongoid (2.0.1), rspec (2.5.0), mongoid-rspec (1.4.2) and fabrication (0.9.5), if necessary.

I'm trying to write my first specs for the People Controller Using mongoid (2.0.1), rspec (2.5.0), mongoid-rspec (1.4.2) and fabrication (0.9.5), if necessary.

(remark: the Organization model mocked inherits from the Person model)

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

I get the following error message when I run this spec:

    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 <t开发者_StackOverflowop (required)>'

My controller is DRY thanks to inherited_resources (1.2.2) and works in development mode as it should.

class PeopleController < InheritedResources::Base
  actions :index
end

Any ideas what I'm doing wrong Mongoid::Criteria object?

Thanks in advance


I had the same problem, but after bundling mongoid-rspec the problem was gone!


I'm having the same problem unfortunately and the only solution I could come up with was to put this in the controller:

def index
  @people = Person.all.to_a
end

The to_a method will convert the Mongoid::Criteria object to an array. This way works for me, but I don't know about how you'd go about it using inherited_resources.


Personally, I gave up with testing inherited_resources index actions with stubbing, and testing returned criteria:

describe "GET 'index'" do
  before do
    get 'index'
  end

  it "returns http success" do
    response.should be_success
  end

  it "should have news" do
    assigns[:news].should be_a(Mongoid::Criteria)
    assigns[:news].selector.should == {}
    assigns[:news].klass.should == News
    assigns[:news].options[:sort].should == [[:created_at, :desc]]
    assigns[:news].options[:limit].should == 10
  end
end


I've recently bumped into similar problem where my controller is returning as expected, but the assigns(:topics) in my rspec is failing (more than actual items returned).

This is inherently due to rspec assigns not being able to load the actual collection resource fetched through Inherited Resources.

The work around I've used it to force load the assigns(:topics) by using logger.info:

class TopicsController < InheritedResources::Base
  def index
    # Needed for rspec assigns() to pass, 
    # since it doesn't evalue inherited_resources resource assignment
    logger.info "Topics: #{@topics.inspect}" if Rails.env.test?
  end
end

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消