Rspec: Should be (this or that)
ActiveSupport provides Object#in?
method. You can combine it with RSpec and simply use the following:
flip_coin.should be_in(["heads", "tails"])
Or with new Rspec 3 syntax:
expect(flip_coin).to be_in(["heads", "tails"])
Is there a difference between 'expect' and 'should' in RSpec?
Quoting from https://github.com/rspec/rspec-expectations/blob/master/Should.md:
From the beginning
RSpec::Expectations
providedshould
and
should_not
methods to define expectations on any object. In version
2.11expect
method was introduced which is now the recommended way to define expectations on an object.
The next paragraph in that document describe the difference in implementation and why expect
is now the recommended approach.
At this point, pretty much anything you can do with should
and should_not
can be done with expect
. See http://rubydoc.info/gems/rspec-expectations/frames for details and expect
usage.
As for your specific code, the reason the first example is failing is that the error is occurring before RSpec has a chance to get involved. You can still use should
to check for the raising of an error, but you have to give RSpec a chance to work. The syntax is lambda {...}.should raise_error
where ...
is the code you're testing.
Rspec: using OR in expect .to eq statement
You can or
two matchers together:
expect(@note.value).to eq(2).or eq(-2)
For more info see.
How to use `or` in RSpec equality matchers (Rails)
How about this:
expect(body['classification'].in?(['Apt', 'Hourse']).to be_truthy
Or
expect(body['classification']).to eq('Apt').or eq('Hourse')
Or even this:
expect(body['classification']).to satify { |v| v.in?(['Apt', 'Hourse']) }
Rspec; How to use should not include?
You can use include
matcher (docs)
expect(object.method('param1')).not_to include(*array)
What exactly is the keyword should in RSpec Ruby
Upon loading, RSpec includes a module into the Kernel
module which is included into all objects known to Ruby. Thus, it can make the should method available to all objects. As such, should
is not a keyword (like if
, class
, or end
) but an ordinary method.
Note that that mixin is only available in RSpec contexts as it is "patched in" during loading or RSpec.
Rails + Rspec: Test that service is called
You're doing it in the wrong order. You need to set the expectation first before the method is expected to be called:
expect_any_instance_of(WeeklyReportCardService).to receive(:send_weekly_report_card_for_repositioning)
put url, params: { check_in: {type_of_weighin: 'standard'}}, headers: { "HTTP_AUTHENTICATION": @token }
If you need to set the expecations afterwards you need to replace the method or object with a spy which is useful if you prefer the arrange-act-assert (or given-when-then)
pattern for structuring tests.
You should also note that the use of any instance is strongly discouraged and you can avoid it by providing a simple class method:
class WeeklyReportCardService
def self.send_weekly_report_card_for_repositioning(...)
new.send_weekly_report_card_for_repositioning(...)
end
end
RSpec.describe API::CheckInsController, type: :request do
it "should send if the client's location is World Wide" do
expect(WeeklyReportCardService).to receive(:send_weekly_report_card_for_repositioning)
put url, params: { check_in: {type_of_weighin: 'standard'}}, headers: { "HTTP_AUTHENTICATION": @token }
end
end
Or alternatively by stubbing the WeeklyReportCardService#new
method to return a mock or spy.
Related Topics
How to Remove Duplicates in a Hash in Ruby on Rails
Str.Each in Ruby Isn't Working
Cannot Install Any Version of Ruby on Mojave - Internal Ranlib Command Failed
How to Change the Default Value of a Struct Attribute
Is It Ever Necessary to Use 'Chomp' Before Using 'To_I' or 'To_F'
How to Upload a Text File and Parse Contents into Database in Ror
Creating a Setter Method That Takes Extra Arguments in Ruby
How to Change the Locale Through Url
How to Enable Chromedriver Logging in Ruby Capybara with Selenium
"No Routes Matches" When Using Current_Page in Rails 3
Can You Install Documentation for Existing Gems
How to Parse a Url and Extract the Required Substring
How to Get a Selenium/Ruby Bot to Wait Before Performing an Action
How to Include Blank Field in Select_Tag
How to Transpose Different Sized Ruby Arrays
Ruby on Rails - Doesn't Create Script/Server
What Does the Term "Vendoring" or "To Vendor" Mean for Ruby on Rails