Rspec - expecting one among specific outputs - ruby

Working on rspec(2.14.1), rspec-core (2.14.8) & rspec-expectations(2.14.5) versions
Need to expect one of two specific outputs. Tried below statements and got respective errors,
expect(fruit).should be_in('Apple','Mango')
Error
NoMethodError: undefined method `should' for #<RSpec::Expectations::ExpectationTarget:0x007fa63c4336d8>
and
expect(fruit).to eq('Apple').or(eq('Mango'))
and
expect(fruit).to include('Apple').or(eq('Mango'))
Error
NoMethodError: undefined method `or' for #<RSpec::Matchers::BuiltIn::Eq:0x007fa63c431630>
Searched a lot but couldn't find a solution. Is there a way to do it without having to update rspec to 3?

Try this:
expect(fruit).to be_in(['Apple','Mango'])
You are expecting an element (fruit) to be in an array (['Apple', 'Mango'])

You can do...
expect(['Apple', 'Mango'].include?(fruit)).to be true

Related

Ruby NoMethodError exception calls #inspect by default

I have found a weird problem in Ruby and I'm not quite sure if it is an issue or it's a feature introduced in recent Ruby versions.
Basically when we call an undefined method, we get an undefined method error as expected in Ruby. The issue is that it also calls inspect and prints out the object and all of its attributes/values. If my object is a complex one, it takes really long time to finish printing everything out and in many times it causes my local rails server to hang, especially when there is an attribute holding binary data. This issue does not seem to happen with Ruby 2.6 but Ruby >= 2.7.
For simple objects, that's not a problem but I'm specifically having the issue with this gem: puppeteer-ruby. Each object in this gem has lots of attributes and dependencies.
Here is an example, I run the following code in IRB with different Ruby version and getting different exceptions:
require 'puppeteer-ruby'
browser = Puppeteer.launch(headless: true)
browser.foo
Ruby 2.6.6
NoMethodError: undefined method `foo' for #<Puppeteer::Browser:0x00007fce2d553b30>
Did you mean? for
Ruby 3.0.0
(irb):9:in `<main>': undefined method `foo' for #<Puppeteer::Browser:0x00007f9300550740 #ignore_https_errors=false, #default_viewport=#<Puppeteer::Viewport:0x00007f92ffbf8e90 #width=800, #height=600, #device_scale_factor=1.0, #is_mobile=false, #has_touch=false, #is_landscape=false>, #process=#<Puppeteer::BrowserRunner::BrowserProcess:0x00007f92fc5ae448 #spawnargs=["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--disable-background-networking", "--enable-features=NetworkService,NetworkServiceInProcess", "--disable-background-timer-throttling", "--disable-backgrounding-occluded-windows", "--disable-breakpad", "--disable-client-side-phishing-detection", "--disable-component-extensions-with-background-pages", "--disable-default-apps", "--disable-dev-shm-usage", "--disable-extensions", "--disable-features=Translate", "--disable-hang-monitor", "--disable-ipc-flooding-protection", "--disable-popup-blocking", "--disable-prompt-on-repost", "--disable-renderer-backgrounding", "--disable-sync", "--force-color-profile=srgb", "--metrics-recording-only", "--no-first-run", "--enable-automation", "--password-store=basic", "--use-mock-keychain", "--enable-blink-features=IdleDetection", "--headless", "--hide-scrollbars", "--mute-audio", "about:blank", "--remote-debugging-port=0", "--user-data-dir=/var/folders/qn/kx4kb8xx5x13gx77yy2q56fr0000gn/T/puppeteer_dev_chrome_profile-20210910-25051-m7mnxb"], #stdout=#<IO:fd 24>, #stderr=#<IO:fd 26>,......very long text after this..
Thats interesting - inspect has always printed internal variables.
In the blame the last change to inspect was 9 years ago: rb_object_inspect#704
A simple script using 2.6:
> docker run -it ruby:2.6.6-alpine sh
#> ruby -v
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux-musl]
#> irb
irb(main):001:0> class Foo; def initialize(message); #message = message; end; end
=> :initialize
irb(main):002:0> Foo.new('hello').foo
NoMethodError (undefined method `foo' for #<Foo:0x00007f35e20e0280 #message="hello">)
Did you mean? for
Though looking at the source for Puppeteer::Browser it seems they changed to a pure ruby implementation of the browser around 2 years ago. When they did that they introduced a lot of ivars to the class. Perhaps the version you're using changed between updating ruby from 2.6 to 3.0?
A simple fix would be to monkey patch inspect on Puppeteer::Browser, or send a PR to the maintainer - looks like the project is active on GitHub.

Minitest: NoMethodError: undefined method `split' for nil:NilClass

This is my test:
test 'accepts nil first_name' do
user = User.new(first_name: nil)
assert_equal(nil, user.first_name)
end
When I run it, I am getting this error from minitest:
NoMethodError: undefined method `split' for nil:NilClass
I can create the user manually in the console and it works, so I'm pretty sure the test should be passing.
Where is this nil.split coming from? My code does not use split anywhere.
Change this:
assert_equal(nil, user.first_name)
To this:
assert_nil(user.first_name)
I didn't dig down the stack deep enough to figure out what was being split where, but this fixed the problem.

Trying to change Ruby Cucumber Transforms Code to Cucumber 3.0.0 Replacement

I recently found this old project for setting up ruby, cucumber, and capybara on this page: jonathanchrisp/capybara-cucumber-ruby-kickstarter
I was able to fix some runtime errors by adding the following line in support/env.rb
require 'capybara/dsl'
However, whenever I run "cucumber" to run the code, it always fails with
undefined method `open_google_maps' for "google maps":String (NoMethodError)
I think the problem is that the "Transforms" code in features/step_definitions/transforms.rb was deprecated in cucumber 3.0.0. The code I am looking at is over 3 years old so I am not sure if the original author will still maintain it. I did reach out to him. In the meantime, I was wondering if there is a Rub/Cucumber expert who can tell me what is the proper way to change the old transform code from:
Transform(/^google maps$/) do |impersonator|
google_maps
end
Transform(/^the user$/) do |impersonator|
#current_user
end
Transform(/^a anonymous user$/) do |impersonator|
a_anonymous_user
end
to something that works in cucumber 3.0.0 and later.
Transforms were removed from Cucumber 3 and you need to swap over to using ParameterTypes. How to do that is documented in the 'Upgrading to Cucumber 3' blog post at https://cucumber.io/blog/2017/09/21/upgrading-to-cucumber-3 .
For your first transform above it would be something like
ParameterType(
name: 'GoogleMaps'
regexp: /google maps/,
transformer: -> (_) { google_maps }
end

method not found for Mandrill ruby gem -- cut and pasting from example

I am using the same code as found in the Mandrill documents:
http://help.mandrill.com/entries/23257181-Using-the-Mandrill-Ruby-Gem
I cut and paste and get the following error at this line:
m = Mandrill::API.new =>
ArgumentError: wrong number of arguments (0 for 1..2)
from /usr/local/lib/ruby/gems/2.1.0/gems/mandrill-0.0.4/lib/mandrill/api.rb:35:in `initialize'
But this is exactly as described in the official docs.
When I, on my own, pass a parameter for the environment variable as follows:
m = Mandrill::API.new(ENV['MANDRILL_APIKEY'])
I get a legitimate client. However, the next line fails:
sending = m.messages.send message
DEBUG -- : HTTPI POST request to mandrillapp.com (excon)
Mandrill::API::Error: (-99) Unknown method "..messages"
from /usr/local/lib/ruby/gems/2.1.0/gems/mandrill-0.0.4/lib/mandrill/api.rb:51:in `method_missing'
Seems like things aren't working. This set of code worked prior. But I'm not sure how to proceed.
It seems like you're not using the official gem called mandrill-api but rather a deprecated 3rd party gem called mandrill. Use the former and the problem should go away.

RSpec view test using RR failing - undefined method stub

I am using Rails 3.1, RSpec 2.6, and rr 1.0.4 and I get a NoMethodError:
undefined method `stub' for #<Activity:0x007faa90996190>
I am trying to utilize the RSpec test below to test my "Activities" show.haml view. If I change my spec_helper.rb file to use RSpec for mocks instead of rr then the test passes. I have tried changing the syntax of the code but to no success.
I found several websites stating that RSpec and rr do not "play well together and one person provided this rpsec-rr solution which did not work for me.
This is my show.haml_spec.rb
require 'spec_helper'
describe "activities/show.haml" do
before(:each) do
#activity = assign(:activity, stub_model(Activity))
end
it "renders attributes in .haml" do
render
end
end
This is the output from my Eclipse compiler using Aptana Studio
Failures:
1) activities/show.haml renders attributes in .haml
Failure/Error: #activity = assign(:activity, stub_model(Activity))
NoMethodError:
undefined method `stub' for #<Activity:0x007faa90996190>
# ./spec/views/activities/show.haml_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.15479 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/views/activities/show.haml_spec.rb:8 # activities/show.haml renders attributes in .haml
Any recommendation to an alternate syntax would be greatly appreciated!!
Note that as of RR 1.0.5 (released 3/28/2013) this problem should no longer be present -- RR is fully compatible with RSpec 2.

Resources