0
votes

I'm working on a gem: https://github.com/tbbooher/govtrack/tree/with_reprah. If I play around in irb, I see the behavior I want:

tbbooher@govtrack:~/workspace (with_reprah) $ irb -Ilib/ 
2.1.4-p265 :001 > require 'govtrack'
 => true 
2.1.4-p265 :002 > bill = GovTrack::Bill.find_by_id(2343)                                                                                                          
 => # c = bill.committees.first
 => # 
2.1.4-p265 :004 > c.class
 => GovTrack::Committee 

But, I want to test this, so I have the following test with rspec-3.1.0:

context 'when i have a bill', :vcr do
    let(:bill){ GovTrack::Bill.find_by_id(2343) }

    # this passes 
    it "should be a Senate Committee on the Judiciary" do
      expect(bill.committees.first.name).to eql("Senate Committee on the Judiciary")
    end

    it "should have a committee of type GovTrack::Committee" do
      expect(bill.committees.first.class.should).to be_a GovTrack::Committee
    end

  end

The result is:

  1) GovTrack::Bill when i have a bill should have a committee of type GovTrack::Committee
     Failure/Error: expect(bill.committees.first.class.should).to be_a GovTrack::Committee
       expected # to be a kind of GovTrack::Committee
     # ./spec/govtrack/bill_spec.rb:68:in `block (3 levels) in '

So the GovTrack::Committee is not of class GovTrack::Committee . . .

Any troubleshooting options appreciated.

1

1 Answers

4
votes

The following is wrong:

expect(bill.committees.first.class.should).to be_a GovTrack::Committee

I think that should is a typo by you. But even without the should, you're basically testing the following:

GovTrack::Committee.class == GovTrack::Committee #=> not right.

Choose one of the following:

expect(bill.committees.first.class).to eq GovTrack::Committee

or the following which is preferable in case of instances of inherited classes:

expect(bill.committees.first).to be_a GovTrack::Committee

Also, just FYI, for a more comprehensive test you should test all the committee objects like so:

bill.committees.each do |committee|
  expect(committee).to be_a GovTrack::Committee
end