0
  describe "changes shopping array length" do
subject { described_class.new.change_array_length(group: [], num: 2) }
it "by responding to a method with arguments" do
  expect(subject).to respond_to(:change_array_length).with(2).arguments
end

This is what I have written so far. However, when I run the test I get:

Failure/Error:
   def change_array_length(group, num)
     group << num
   end
 
 ArgumentError:
   wrong number of arguments (given 1, expected 2)

I removed the num argument and the number of arguments goes down to 0 so it's not reading the empty array.

I've just started learning testing with a course using Ruby not RoR so let me know if I have this slightly wrong for testing the method with arguments!

Thanks!

2 Answers 2

1

Based on expectation to verify respond_to, subject should be instance of described class not the the method call.

describe "changes shopping array length" do
  subject { described_class.new }
  it "by responding to a method with arguments" do
    expect(subject).to respond_to(:change_array_length).with(2).arguments
  end
end

Also one more thing based on your method definition, method does not seem to used named arguments, but when you are calling in subject you are passing as named arguments which @Aarthi mentioned in her answer.

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

2 Comments

Oh okay, so if I wanted to say check the group being passed into the method was empty... How would I test for that? Appreciate the help both of you =)
0
subject { described_class.new.change_array_length([], 2) }

You used named arguments which will be considered as a hash(single argument). Pass like an unnamed argument as defined in the method.

1 Comment

Hmm that doesn't seem to work. It now gives an error: Failure/Error: expect(subject).to respond_to(:change_array_length).with(2).arguments expected [2] to respond to :change_array_length with 2 arguments. The way I originally had it reads it as at least 1 argument?

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.