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

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.

Related

NoMethodError - undefined method `now' for Watir::Time:Class

I get the following error anytime I try to interact with a Watir element.
/Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/wait/timer.rb:40:in `current_time': undefined method `now' for Watir::Time:Class (NoMethodError)
from /Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/wait/timer.rb:6:in `initialize'
from /Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/elements/element.rb:656:in `new'
from /Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/elements/element.rb:656:in `element_call'
from /Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/elements/element.rb:114:in `click'
from fund_cc.rb:8:in `<main>'
Here is my code:
require 'watir'
# require 'time'
b = Watir::Browser.new(:chrome)#, :url => "http://localhost:9515")
b.goto "https://www.bankofamerica.com/"
contact_us= b.link(:text, "Contact Us")
contact_us.click
Does anyone know how to resolve this?
This should be fixed in version 6.0.2.
From Titus Fortner on the Watir-General mailing list:
The latest version of Watir attempts to use monotomic time where
supported and it looks like we grabbed the wrong Time class for where
it is not supported.
I just updated and pushed the fix to 6.0.2. You should be able to just
bundle update now.
I was able to reproduce this behavior. You can monkey patch your gem locally by tweaking the current_time method in /lib/watir/wait/timer.rb:
def current_time
::Time.now.to_f # was Time.now.to_f
end
And I'd suggest logging an issue on https://github.com/watir/watir/issues.

Rspec - expecting one among specific outputs

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

ruby unit testing at_start not found

I am using test-unit-2.5.5 with ruby 1.9.3. In http://test-unit.rubyforge.org/test-unit/en/Test/Unit.html#at_start-class_method there is a method called at_start as part of the ruby test::unit module from version 2.5.2. I tried to use it from the examples on the page like so:
class TestAOS < Test::Unit::TestCase
Test::Unit.at_start do
puts "start"
end
Test::Unit.at_exit do
puts "Exit!"
end
But when I run my test I get the following:
NoMethodError: undefined method `at_start' for Test::Unit:Module
TestAOS at unit/TestAOS.rb:8
(root) at unit/TestAOS.rb:7
Do I need to do anything first before this method can be used? I'm new to ruby
When I comment out the at_start bloack and run the test I get a different error for at_exit:
NoMethodError: private method `at_exit' called for Test::Unit:Module
TestAOS at unit/TestAOS.rb:12
(root) at unit/TestAOS.rb:7
A
In the example provided by your link the
Test::Unit.at_start do
puts "start"
end
is called outside of test class. You are calling it from inside of your test class. Just move it outside of your TestAOS

Rspec tests not running because of initializer Yaml files

I'm integrating tests with Rspec into quite a large / developed app. When I run the test I'm getting very odd errors from some of my initializers.
For example, my carrierwave.rb loads a yaml file, when I run the test command I get:
carrierwave.rb:11:in `block in <top (required)>': undefined method `[]' for nil:NilClass
which is referring to the second line of code below:
fog_config = YAML::load_file(Rails.root.join 'config/fog.yml')[Rails.env.to_s]
config.fog_directory = fog_config['directory']
Momentarily if I remove that line of code in carrierwave, omniauth starts to complain too:
omniauth.rb:4:in `block in <top (required)>': undefined method `symbolize_keys' for nil:NilClass (NoMethodError)
Again, another yaml file:
fb_config = YAML::load_file(Rails.root.join 'config/fb_app_version.yml')[Rails.env].symbolize_keys
I'm guessing the solution is to change my yaml files or include something in the tests, but I'm not sure what. Any ideas would be most appreciated, thanks.
It looks like you need to include test entries in the Yaml files in question.
For example, your config/fog.yml probably looks something like this:
production:
directory: the_prod_directory
other_keys: ...
development:
directory: the_dev_directory
other_keys: ...
You need to add a test key:
test:
directory: the_test_dir
other_keys: appropriate values

undefined method `[]' for nil:NilClass (NoMethodError)

i am using sequel gem and postgresql database
i have a file hello.rb with code
require "rubygems"
require './post'
require "sequel"
# connect to an in-memory database
DB = Sequel.connect('postgres://ritesh:newpassword#localhost')
post = Post['ruby1', 'hello world1']
post[1]
and i created a table post and
a model with code
require 'sequel'
DB = Sequel.connect('postgres://ritesh:newpassword#localhost')
class Post < Sequel::Model
set_primary_key [:category, :title]
end
when i run command ruby hello.rb .i am getting the following error
hello.rb:10: undefined method `[]' for nil:NilClass (NoMethodError)
first thing i want to know for creating we should create a table with that name and then create a model??
second thing if first assumption is true then why i am getting this error??
To construct a new object in ruby you should call new. I am not sure what is post but it seems the correct syntax should be:
post = Post.new('ruby1', 'hello world1')
Otherwise post will remain null after this line.

Resources