Skip to content

Instantly share code, notes, and snippets.

@cadwallion
Created June 25, 2012 15:09
Show Gist options
  • Save cadwallion/2989188 to your computer and use it in GitHub Desktop.
Save cadwallion/2989188 to your computer and use it in GitHub Desktop.
RSpec include_kind_of
require 'spec_helper'
module Bar
def bar ; end
end
class Foo
include Bar
end
describe 'include_kind_of' do
it 'should return true if array contains object of kind_of expected' do
ary = [Foo.new]
ary.should include_kind_of Bar
end
it 'should return false if array contains a class of expected' do
ary = [Foo]
ary.should_not include_kind_of Bar
end
end
RSpec::Matchers.define :include_kind_of do |expected|
match do |actual|
actual.count { |c| c.kind_of? expected } > 0
end
failure_message_for_should do |actual|
"expected to include kind of #{expected}, found none."
end
end
@hypomodern
Copy link

What I did was similar:

actual.map(&:class).should include(expected)

@cadwallion
Copy link
Author

Those do two different things. kind_of? doesn't just look at the class, but the class hierarchy and module inclusion.

@hypomodern
Copy link

Yeah, thus "similar" :). I think I'm just going to sub yours in here.

@hypomodern
Copy link

This is interesting and unexpected:

"expected [#<TestWidget:0x00000100fd1e38 @embedded=#<RSpec::Mocks::Mock:0x807e7b58 @name=nil>>] to include kind of TestWidget, found none."

probably a reloading issue?

@cadwallion
Copy link
Author

Doh. I'm pretty sure I know the problem, will test to verify.

@cadwallion
Copy link
Author

kind_of works on objects, not classes. Updated gist to reflect this disparity, but I'll see if I can meet both cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment