1

I have a normal Rails project (without Active Record) using CouchDB (couchrest_model) as a document database.

I did setup RSpec and a basic scaffold 'Project'.

When I test the following spec:

  before(:each) do
    @project = Project.create! valid_attributes
  end

  describe "GET index" do
    it "assigns all projects as @projects" do
      get :index
      assigns(:projects).should eq([@project])
    end
  end

I get the following error:

Failure/Error: assigns(:projects).should eq([@project])

       expected [{"name"=>"test", 
                  "updated_at"=>2011-05-28 11:24:04 -0500,
                  "created_at"=>2011-05-28 11:24:04 -0500, 
                  "couchrest-type"=>"Project",
                  "_id"=>"709edbfaafb24fa1eff7d5f3966b2bda",
                  "_rev"=>"1-ae3f6356f8e32f6006337a4f3759bca4"}]
            got [{"name"=>"test",
                  "updated_at"=>2011-05-28 11:24:04 -0500, 
                  "created_at"=>2011-05-28 11:24:04 -0500,
                  "_id"=>"709edbfaafb24fa1eff7d5f3966b2bda",
                  "_rev"=>"1-ae3f6356f8e32f6006337a4f3759bca4",
                  "couchrest-type"=>"Project"}]

It seems that the only difference is the order of the elements in the hash:

  (compared using ==)

   Diff:

   @@ -1,7 +1,7 @@
    [{"name"=>"test",
      "updated_at"=>2011-05-28 11:24:04 -0500,
      "created_at"=>2011-05-28 11:24:04 -0500,
   -  "couchrest-type"=>"Project",
      "_id"=>"709edbfaafb24fa1eff7d5f3966b2bda",
   -  "_rev"=>"1-ae3f6356f8e32f6006337a4f3759bca4"}]
   +  "_rev"=>"1-ae3f6356f8e32f6006337a4f3759bca4",
   +  "couchrest-type"=>"Project"}]

I know 'rspec' and 'rspec-rails' only work out of the box for Active Record, but it shouldn't be so different for other ORMs. Am I missing something?

Which is the best way to fix this test?

Christian
  • 1,872
  • 1
  • 14
  • 14

1 Answers1

0

Try:

assigns(:projects).should == [@project]
Andy Waite
  • 10,785
  • 4
  • 33
  • 47
  • Are you using Ruby 1.9? If so, can you try it in 1.8? – Andy Waite May 30 '11 at 18:41
  • Yes, I'm using ruby 1.9.2. I tried with 1.8.7 and it didn't work, same error. I'm also using rails 3.0.7, rspec 2.6.0, rspec-rails 2.6.1. I think it is an issue with RSpec's 'assigns' – Christian May 30 '11 at 20:46
  • Can you check the class of each object, i.e. puts assigns(:projects).first.class; puts @project.class – Andy Waite May 30 '11 at 20:54
  • Both classes are the same: "Project"... but the order of elements is different when you inspect them... – Christian May 30 '11 at 21:13
  • At the end this was a bug with CouchRest Model which was fixed recently. https://github.com/couchrest/couchrest_model/issues/82 Anyway, your answer led me to check more in detail RSpec. – Christian Jun 28 '11 at 20:46