0

I try to test method, what will be happen if coming arguments sequence is incorrect. i have already checked for nil parameter. my method accept id, new_name and i want to know what to do when parameters are new_name, id

Hope my question is clear.

Method which i want to test:

def update_recipe(id, new_name)
  raise ArgumentError if id.nil? || new_name.nil?
  recipe = find_recipe_by_id(id).dup
  recipe.name = new_name
  @storage[id] = recipe
  recipe.freeze
end

Here is the rspec code:

context 'when argument sequence is not correct' do
  let(:added_recipe) { subject.add_recipe recipe }

  it 'raises an ArgumentError' do
    expect { subject.update_recipe 'Pasta', added_recipe.id }.to raise_error ArgumentError
  end
end
2
  • If you are worried about this, use Keyword Arguments: robots.thoughtbot.com/ruby-2-keyword-arguments Commented Jul 27, 2016 at 15:03
  • Hash parameters That's what i exactly needed thank you very much! Commented Jul 28, 2016 at 6:36

1 Answer 1

1

You could do something similar to what you did for handling nil based on what you know id or new_name should be. For example, making sure the id is an integer.

def update_recipe(id, new_name)
  raise ArgumentError unless id.is_a? Integer
  raise ArgumentError if id.nil? || new_name.nil?
  recipe = find_recipe_by_id(id).dup
  recipe.name = new_name
  @storage[id] = recipe
  recipe.freeze
end
Sign up to request clarification or add additional context in comments.

5 Comments

Yea but here the problem is what if i send argument like def_update new_name, id not as id, new_name ?
it might be helpful if you show what your concerns are in code
Method expect 2 parameter in this order. def update_recipe id, new_name end So what if i use a method like .update_recipe new_name, id
Did you notice I changed your code? My changes will make your test pass.
Yea but im not talking about type checking. lets check my rspec expect code i have send prameters in incorrect order not as method expected. So this must raise an error ?

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.