Getting below error message when running Rspec after upgrading Ruby to 3.0.3
Failure/Error: FactoryBot.lint strategy: :build_stubbed
FactoryBot::InvalidFactoryError:
The following factories are invalid:
user - wrong number of arguments (given 1, expected 0; required keywords: event_type, object, summary, author) (ArgumentError)
Related
Env :
Ruby : 2.6.0
Rails : 6.1.0
Scenario :
In my project, I have a model Product like below :
# encoding: utf-8
class Product < ApplicationRecord
validates_presence_of :name
...
default_scope -> { order("name ASC")}
end
In console I got error
*** ArgumentError Exception: wrong number of arguments (given 2, expected 1)
when I do simply this :
(byebug) Product.new
*** ArgumentError Exception: wrong number of arguments (given 2, expected 1)
I saw that the behaves of default_scope has been changed in Rails 6.1 by adding **all_queries** PR in Rails But I don’t understand. This should have nothing to do with me.
Any one have same problem ?
I'm facing joins association issue in Rails 5.2.1, below query working properly in rails 5.2.0 but not in 5.2.1
User.joins(:roles).where(email: 'test#gmail.com', roles: {titles: ['test1', 'test2']})
Here relation between user and roles had has_many, following error I'm getting while running this query in rails console.
ArgumentError: wrong number of arguments (given 3, expected 2)
from /Users/testuser/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.1/lib/active_record/associations/join_dependency/join_association.rb:12:in `initialize'
Since I updated my Gemfile and moved to rspec 3, in many tests, I'm getting a error for: way:
it "should reject attribute that are too short" do
short = "a" * 3
hash = #attr.merge(:details => short)
Deal.new(hash).should have(1).error_on(:details)
end
I'm getting this error:
Failure/Error: Deal.new(hash).should have(1).error_on(:details)
NoMethodError:
undefined method `have' for #<RSpec::ExampleGroups::Deal_2::TestsOnDealsModelsValidations>
I read I should now be using "expect" instead of should but here with have(1).error_on, how should I write it to comply with rspec 3?
I tried the following but it still does not work:
it "should reject attribute that are too short" do
short = "a" * 3
hash = #attr.merge(:details => short)
expect(Deal.new(hash).error_on(:details).size).to eq(1)
end
I have replaced the likes of
Deal.new(hash).should have(1).error_on(:details)
with
deal = Deal.new(hash)
expect(deal.valid?).to be_falsey
expect(deal.errors[:details].size).to eq(1)
The first expectation with valid? is necessary as it initializes the errors list.
have and other similar matchers have been moved out of rspec core and into another gem, rspec-collection-matchers.
I recommend following the upgrade path from rspec 2 -> 3 as detailed in the rspec docs: https://relishapp.com/rspec/docs/upgrade
Upgrade to rspec 2.99
Run your test suite
Fix deprecation warnings
Upgrade to rspec 3.
If you had done this you would have received a deprecation error with your code that would have also told you what to do to fix it.
The line to add to your Gemfile should be:
gem 'rspec-collection_matchers'
This is my code:
before(:each) do
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(api_key.access_token)
end
My test was supposed to pass but it is displaying this error:
Failure/Error: request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials("#{api_key.access_token}")
ArgumentError:
wrong number of arguments (0 for 1..2)
Gems:
rspec-rails version = 2.14.1.
rails verison = 4
mongoid from 'git://github.com/mongoid/mongoid.git'
The request method you are accessing in your test is documented at http://rubydoc.info/gems/rack-test/0.6.2/Rack/Test/Session:request and expects 1 or 2 arguments as the error indicates. You're getting an error on request.env because you're invoking request with no arguments.
My test example is:
it "routes to #add_role" do
post("/users/1/add_role").should route_to("users#add_role",id: 1)
end
Here is the failure message:
UsersController routing routes to #add_role
Failure/Error: post("/users/1/add_role").should route_to("users#add_role",id: 1)
The recognized options <{"controller"=>"users", "action"=>"add_role", "id"=>"1"}> did not match <{"id"=>1, "controller"=>"users", "action"=>"add_role"}>, difference: <{"id"=>1}>.
<{"id"=>1, "controller"=>"users", "action"=>"add_role"}> expected but was
<{"controller"=>"users", "action"=>"add_role", "id"=>"1"}>.
My environment:
ruby-1.9.3-p194
Rails 3.2.6
Rspec (2.10.0)
You need id in the route_to to be string.
Rails does not type cast into numbers as it makes no assumptions about types