Created
June 25, 2012 15:09
-
-
Save cadwallion/2989188 to your computer and use it in GitHub Desktop.
RSpec include_kind_of
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Those do two different things. kind_of?
doesn't just look at the class, but the class hierarchy and module inclusion.
Yeah, thus "similar" :). I think I'm just going to sub yours in here.
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?
Doh. I'm pretty sure I know the problem, will test to verify.
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
What I did was similar: