Last active
October 8, 2019 22:21
-
-
Save cpkenn09y/287626c8c03bcd7834f266e4ecf00fb0 to your computer and use it in GitHub Desktop.
This file contains 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
## IMPORTANT -> The expectation must come before the invocation, so that it can catch the invocation when it happens | |
# Able to stub an instance's method. Will return whatever is in the block. The instance's method would have to be invoked in the RSpec. | |
@person.stub(:get_relevant_experts) { @collection_of_possibilities } | |
# Expectation of an instance that is available in the testing context to receive a particular method, choose return value | |
expect(@person).to receive(:get_relevant_experts).with("Medical").and_return(@collection_of_possibilities) | |
# Expectation of an instance to receive a method and return a result | |
expect_any_instance_of(Classification).to receive(:valid_matches).and_return(@collection_of_possibilities) | |
# Expectation of a class to receive a method | |
expect(ClassificationSystem).to receive(:choose_record_with_special_flag_or_unclassified_fallback) | |
# How to stub a constant within a class | |
stub_const("Api::V1::PublicationsController::DEFAULT_PAGINATION_COUNT", 1) |
# To allow expectations of 0 to N number of calls, the follow syntax allows them all
allow(FaradayUtility::Content).to receive(:add_survey).and_return(:success)
If you ever find that you are passing in a Boolean into a POST or PUT, and you notice that your Boolean is getting turned into a String...
For example:
true
-> becoming a String of “true”
**Make sure to use request.content_type = 'application/json'
in your spec
Incase you see other specs that break intermittently...
It has a decently high likelihood to be due to:
- expecting that AR will automatically return records in order of id (
self.survey_results
.vs.self.survey_results.order(:id)
) - DateTime objects needing to be stubbed properly
- ENV variables getting set in a spec, then specs after are adversely affected (eg. settings.yml values, Rails.env, or other initializer values)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:: Given the scenario ::
FAILED: expect_any_instance_of(Card).to receive(:post_to_firebase).exactly(5).times # the clause "exactly" does not work for expect_any_instance_of
Success via: expect(FirebaseCard).to receive(:push).exactly(5).times