On Oct 23, 2010, at 5:40 PM, Katrina wrote:

> I have a test that looks like this:
> 
> describe ChannelsController, "GET edit" do
>  it "assigns the greetings summary" do
>    summary = {:total => 139, :submitted => 97, :published =>
> 82, :removed => 12}
>    Greeting.stub(:summary).and_return summary
>    get :index
>    assigns(:summary).should eq(summary)
>  end
> end
> 
> It fails with the following:
> expected {:total=>139, :submitted=>97, :published=>82, :removed=>12}
>       got {"total"=>139, "submitted"=>97, "published"=>82,
> "removed"=>12}
> 
> The code that is getting called is this:
> 
> # ChannelsController
> def index
>  @summary = Greeting.summary
> end
> 
> # Greeting
> def self.summary
>  {
>    :total => self.count,
>    :submitted => self.submitted.count,
>    :published => self.published.count,
>    :removed => self.removed.count
>  }
> end

The definition of self.summary shouldn't matter here since it's being stubbed 
in the example.

> I expected this example to pass,

I would have too, but it looks like Rails is converting hashes that get 
assigned to views to HashWithIndifferentAccess, which only looks at Strings in 
its implementation of ==.

Take a look at http://gist.github.com/642878.

The problem here is that it would be unwise for HashWithIndifferentAccess to 
implement == any differently, because you'd end up with surprising results (a 
== b would pass, but b == a would fail).

So you have to assume that the keys are Strings regardless of what you set in 
the example. Therefore, I'd recommend setting Strings in the example in order 
to keep sane :)

HTH,
David

> and I can't figure out if I should be
> doing something differently either in the code itself or in the test.
> 
> Any insights?
> 
> Thanks,
> Katrina


_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to