ruby Minitest 错误 - ArgumentError:参数数量错误(给定 1,预期为 0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35463812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:47:43  来源:igfitidea点击:

Minitest Error - ArgumentError: wrong number of arguments (given 1, expected 0)

ruby

提问by Dan

I am doing an exercise on exercism.io. This is requested in the Spec document:

我正在 exercism.io 上做练习。这是在 Spec 文档中要求的:

  1. The Hello World!program will greet me, the caller.
  2. If I tell the program my name is Alice, it will greet me by saying "Hello, Alice!".
  3. If I neglect to give it my name, it will greet me by saying "Hello, World!"
  1. Hello World!程序将向我打招呼,来电者。
  2. 如果我告诉程序我的名字是 Alice,它会说“你好,Alice!”来迎接我。
  3. 如果我忘记给它起名字,它会说“你好,世界!”来迎接我。


class HelloWorldTest < Minitest::Test
  def test_no_name
    assert_equal 'Hello, World!', HelloWorld.hello
  end

  def test_sample_name
    assert_equal 'Hello, Alice!', HelloWorld.hello('Alice')
  end

  def test_other_sample_name
    assert_equal 'Hello, Bob!', HelloWorld.hello('Bob')
  end
end

This is my program:

这是我的程序:

class HelloWorld
  def self.hello
    "Hello, World!"
  end

  def initialize(name)
    @name = name
  end

  def say_hello
    puts "Hello, #{@name}!"
  end
end

print "Give me your name: "
your_name = gets.chomp
hello = HelloWorld.new(your_name)
if your_name == ""
  puts "Hello, World!"
else
  hello.say_hello
end

The program runs and satisfies all of the requirements, but I get the error:

该程序运行并满足所有要求,但出现错误:

1) Error:
HelloWorldTest#test_sample_name:
ArgumentError: wrong number of arguments (given 1, expected 0)
    /Users/drempel/exercism/ruby/hello-world/hello_world.rb:3:in `hello'
    hello_world_test.rb:24:in `test_sample_name'

3 runs, 1 assertions, 0 failures, 1 errors, 1 skips

How do I define a method that doesn't require arguments?

如何定义不需要参数的方法?

回答by sawa

How do I define a method that doesn't require arguments?

如何定义不需要参数的方法?

Your problem is the opposite. You are passing an argument to a method HelloWorld.hello, which does not take an argument.

你的问题正好相反。您正在将参数传递给方法HelloWorld.hello,该方法不接受参数。

Your test code does not match what your source code is doing. Either change your source code to:

您的测试代码与您的源代码正在执行的操作不匹配。将您的源代码更改为:

class HelloWorld
  def self.hello(name = "World")
    "Hello, #{name}!"
  end
end

Here, the name = "World"indicates that the nameargument is optional, and that the default value is "World".

这里,name = "World"表示name参数是可选的,默认值为"World"

Or, change your tests to:

或者,将您的测试更改为:

assert_equal 'Hello, Alice!', HelloWorld.new('Alice').say_hello
assert_equal 'Hello, Bob!', HelloWorld.new('Bob').say_hello

回答by SteveTurczyn

Sometimes in a test you say

有时在测试中你说

HelloWorld.hello('Alice')

Other times in a test you say

在测试中的其他时候你说

HelloWorld.hello

So you're calling the method "hello" with and without arguments.

因此,您正在使用和不使用参数调用方法“hello”。

To make arguments optional, you can give them a default value.

要使参数可选,您可以给它们一个默认值。

def self.hello(name_to_use=nil)
  if name_to_use
    "Hello, #{name_to_use}!"
  else
    "Hello, World!"
  end
end

回答by chemturion

How do I define a method that doesn't require arguments?

如何定义不需要参数的方法?

It's important to understand what the error message means, as I had a similar problem on Learn.co's Object-Oriented TicTacToe challenge. When your code is ran against the test code, the test code is "giving" one argument of 'Alice'to the #self.hellomethod. But your #self.hellomethod as written is expecting exactly zero arguments. Hence:

了解错误消息的含义很重要,因为我在 Learn.co 的面向对象的井字游戏挑战中遇到了类似的问题。当您的代码针对测试代码运行时,测试代码“给”'Alice'#self.hello方法的一个参数。但是您#self.hello编写的方法期望完全为零参数。因此:

ArgumentError: wrong number of arguments (given 1, expected 0)
/Users/drempel/exercism/ruby/hello-world/hello_world.rb:3:in `hello'
hello_world_test.rb:24:in `test_sample_name'

My error message was similar and I had the same train of thought: "If it expected 0, how can I give it 0?" Clearly, a key takeaway from our experience is to better understand now what the error message meant.

我的错误信息很相似,我也有同样的想法:“如果它期望为 0,我怎么能给它 0?” 显然,从我们的经验中得出的一个关键结论是现在更好地理解错误消息的含义。