Rails minitest check if record was created - ArgumentError: wrong number of arguments
Rails minitest check if record was created - ArgumentError: wrong number of arguments
我有一个简单的服务,它创建了一个名为 PropertyReport
:
的记录
#app/services/portfolios/related_property_reports.rb
module Portfolios
class RelatedPropertyReports
def initialize(portfolio, portfolio_report)
@portfolio = portfolio
@portfolio_report = portfolio_report
end
def call
PropertyReport.create!(
property: property,
portfolio_report: portfolio_report,
)
end
end
end
现在我想使用以下 minitest 测试此服务:
#app/services/portfolios/related_property_reports_test.rb
require 'test_helper'
module Portfolios
class RelatedPropertyReports < ActiveSupport::TestCase
setup do
@portfolio = Portfolio.create(name: Faker::Bank.name)
@property = Property.create(portfolio: @portfolio, name: Faker::Bank.name, status: 'planned')
@portfolio_report = PortfolioReport.create(portfolio: @portfolio)
end
test 'create new record' do
service.call
assert_equal 1, PropertyReport.count
end
private
def service
@service ||= ::Portfolios::RelatedPropertyReports.new(@portfolio, @portfolio_report)
end
end
end
这给我一个错误:
Portfolios::RelatedPropertyReports#test_create_new_record:
ArgumentError: wrong number of arguments (given 2, expected 1)
考虑到我在 new
中给出了两个参数,这太奇怪了。我错过了什么?
您需要为测试使用不同的 class 名称或模块 class。现在您正在覆盖原始 class 对象。
irb(main):001:1* module Test
irb(main):002:2* class Test
irb(main):003:3* def initialize(a, b)
irb(main):004:2* end
irb(main):005:1* end
irb(main):006:0> end
=> :initialize
irb(main):007:1* module Test
irb(main):008:2* class Test
irb(main):009:3* def initialize(a)
irb(main):010:2* end
irb(main):011:3* def service
irb(main):012:3* ::Test::Test.new(1,2)
irb(main):013:2* end
irb(main):014:1* end
irb(main):015:0> end
=> :service
irb(main):016:0> Test::Test.new(1).service
(irb):9:in `initialize': wrong number of arguments (given 2, expected 1) (ArgumentError)
from (irb):12:in `new'
from (irb):12:in `service'
from (irb):16:in `<main>'
from /home/drewb/.rubies/ruby-3.0.1/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
from /home/drewb/.rubies/ruby-3.0.1/bin/irb:23:in `load'
from /home/drewb/.rubies/ruby-3.0.1/bin/irb:23:in `<main>'
我有一个简单的服务,它创建了一个名为 PropertyReport
:
#app/services/portfolios/related_property_reports.rb
module Portfolios
class RelatedPropertyReports
def initialize(portfolio, portfolio_report)
@portfolio = portfolio
@portfolio_report = portfolio_report
end
def call
PropertyReport.create!(
property: property,
portfolio_report: portfolio_report,
)
end
end
end
现在我想使用以下 minitest 测试此服务:
#app/services/portfolios/related_property_reports_test.rb
require 'test_helper'
module Portfolios
class RelatedPropertyReports < ActiveSupport::TestCase
setup do
@portfolio = Portfolio.create(name: Faker::Bank.name)
@property = Property.create(portfolio: @portfolio, name: Faker::Bank.name, status: 'planned')
@portfolio_report = PortfolioReport.create(portfolio: @portfolio)
end
test 'create new record' do
service.call
assert_equal 1, PropertyReport.count
end
private
def service
@service ||= ::Portfolios::RelatedPropertyReports.new(@portfolio, @portfolio_report)
end
end
end
这给我一个错误:
Portfolios::RelatedPropertyReports#test_create_new_record: ArgumentError: wrong number of arguments (given 2, expected 1)
考虑到我在 new
中给出了两个参数,这太奇怪了。我错过了什么?
您需要为测试使用不同的 class 名称或模块 class。现在您正在覆盖原始 class 对象。
irb(main):001:1* module Test
irb(main):002:2* class Test
irb(main):003:3* def initialize(a, b)
irb(main):004:2* end
irb(main):005:1* end
irb(main):006:0> end
=> :initialize
irb(main):007:1* module Test
irb(main):008:2* class Test
irb(main):009:3* def initialize(a)
irb(main):010:2* end
irb(main):011:3* def service
irb(main):012:3* ::Test::Test.new(1,2)
irb(main):013:2* end
irb(main):014:1* end
irb(main):015:0> end
=> :service
irb(main):016:0> Test::Test.new(1).service
(irb):9:in `initialize': wrong number of arguments (given 2, expected 1) (ArgumentError)
from (irb):12:in `new'
from (irb):12:in `service'
from (irb):16:in `<main>'
from /home/drewb/.rubies/ruby-3.0.1/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
from /home/drewb/.rubies/ruby-3.0.1/bin/irb:23:in `load'
from /home/drewb/.rubies/ruby-3.0.1/bin/irb:23:in `<main>'