Test doubles are any object that are suppose to stand in and represent real objects during testing. Rspec offers three types of doubles - ordinary doubles(just double), instance doubles, and class doubles.
All doubles are strict by default. What that means is that if any un-allowed or unexpected methods are invoked, the test will fail. For example:
x = double()
a.bar // error because `bar` was not allowed.
However, you can make any double loose by appending `as_null_object`.
Now lets look at differences.
Ordinary doubles
x = double()
These doubles are super barebones. You can allow any messages on these methods.
Instance doubles
x = instance_double('ClassName')
These doubles are aware of the instance methods of class 'ClassName' - you can only allow messages that are defined.
Class doubles
x = class_double('ClassName')
These doubles are aware of the class / module methods of any class or module named 'ClassName'. Just like instance doubles, only defined messages are allowable.
In short, instance and class doubles go a step farther than verifying the state of an object (was 'X' called?). They also verify behavior (is 'X' a thing that this object does?).
When do you use one over the other?
I see ordinary doubles as good for creating dummy objects during a test. For example, if you need to fulfill a parameter requirement for a method where you know the object isn't being used. However, you generally want to use instance and class doubles for the stricter check.
I was diagnosed as HEPATITIS B carrier in 2013 with fibrosis of the
ReplyDeleteliver already present. I started on antiviral medications which
reduced the viral load initially. After a couple of years the virus
became resistant. I started on HEPATITIS B Herbal treatment from
ULTIMATE LIFE CLINIC (www.ultimatelifeclinic.com) in March, 2020. Their
treatment totally reversed the virus. I did another blood test after
the 6 months long treatment and tested negative to the virus. Amazing
treatment! This treatment is a breakthrough for all HBV carriers.