1

I'm trying to write unit tests for my code using rspec. I keep getting a "wrong number of arguments" error:

class MyClass
  attr_accessor :env, :company,:size, :role, :number_of_hosts,:visability

  def initialize(env, company, size, role, number_of_hosts, visability)
    @env, @company, @size, @role, @number_of_hosts, @visability =  env, company, size, role, number_of_hosts, visability
  end




end

And here are my tests:

require_relative "../lib/MyClass.rb"

 describe MyClass do
    it "has an environment" do
        MyClass.new("environment").env.should respond_to :env
    end

    it "has a company" do
        MyClass.new("company").company.should respond_to :company
    end

...

When I run rspec I get:

1) MyClass has an environment
     Failure/Error: MyClass.new("environment").env.should respond_to :env
     ArgumentError:
       wrong number of arguments (1 for 6)
     # ./lib/MyClass.rb:4:in `initialize'
     # ./spec/MyClass_spec.rb:5:in `new'
     # ./spec/MyClass_spec.rb:5:in `block (2 levels) in <top (required)>'

...

What am I missing?

EDIT

Sergio helped thanks...however

Sergio's answer worked...although I still have a further question:

Given the Class:

class Team
    attr_accessor :name, :players

    def initialize(name, players = [])
        raise Exception unless players.is_a? Array

        @name = name
        raise Exception if @name && has_bad_name

        @players = players
    end

    def has_bad_name
        list_of_words = %w{crappy bad lousy}
        list_of_words - @name.downcase.split(" ") != list_of_words
    end

    def favored?
        @players.include? "George Clooney"
    end

end

and spec...

require_relative "../lib/team.rb"

describe Team do
    it "has a name" do
        Team.new("random name").should respond_to :name
    end

    it "has a list of players" do
        Team.new("random name").players.should be_kind_of Array
    end

...

The tests pass without the same error...(This works fine: Team.new("random name"))

Any explanation?

6
  • 2
    You're missing 5 more arguments to the constructor. Commented Sep 21, 2013 at 22:53
  • you mean like, MyClass.new("company","blah","blah","blah","blah") ?? Commented Sep 21, 2013 at 22:55
  • 1
    Yeah, something like that Commented Sep 21, 2013 at 22:55
  • I edited my question...can you explain why I needed to add multiple arguments? Commented Sep 21, 2013 at 23:07
  • 4
    -1 for changing the question after posting it instead of posting a new question for the additional question you've had. That's considered rude here. Commented Sep 22, 2013 at 4:47

1 Answer 1

3

Here is the error MyClass.new("environment"). As you have written def initialize(env, company, size, role, number_of_hosts, visability). So you should pass 6 parameters when you are calling MyClass#new method. But in practice you pass only one which is "environment". Thus you got the legitimate error - wrong number of arguments (1 for 6).

Sign up to request clarification or add additional context in comments.

11 Comments

Yeah, that's what rspec said... mind explaining why he got the error (hint, there is a comment that already says that)
@BenjaminGruenbaum Comment is not an answer,, its a hint.. right? I think my answer is appropriate for this post.. But don't know why so many down-votes..
Hmmm can't cancel the down vote yet, all it said when I read it was the first sentence. :(
@ArupRakshit I receded my down vote as soon as you edited your answer to include information about the problem over the message OP got.
@ArupRakshit: On the other hand, you shouldn't be posting half-baked (even less than so) answers with typos just to claim first answer. Maybe that explains some downvotes. I like to be first answerer too, but I strive to post usable answers in revision 0.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.