我需要一种方法来判断一个对象是否是使用RSpec的另一个对象的实例.例如:
describe "new shirt" do
it "should be an instance of a Shirt object"
# How can i check if it is an instance of a shirt object
end
end
我需要一种方法来判断一个对象是否是使用RSpec的另一个对象的实例.例如:
describe "new shirt" do
it "should be an instance of a Shirt object"
# How can i check if it is an instance of a shirt object
end
end
首选语法是:
expect(@object).to be_a Shirt
较旧的语法是:
@object.should be_an_instance_of Shirt
请注意,两者之间有一个非常微妙的区别.如果衬衫继承自服装,那么both of these expectations will pass:
expect(@object).to be_a Shirt
expect(@object).to be_a Garment
如果你做了,并且@object是一件衬衫,那么second expectation will fail:
@object.should be_an_instance_of Shirt
@object.should be_an_instance_of Garment