0

I'm testing a model that receives a optional parameter and Rspec is giving me an error:

no implicit conversion of Symbol into Integer

My route:

get "classification/teams(/:team_id)" => "classifications#teams"

My model:

class Classification
def self.teams(params = [])
    team_id = params[:team_id] unless params[:team_id].nil?
    ...

My test:

it 'returns the classification' do
    expect(Classification.teams).to be_true
end

My API is working well, but I need to implement some tests and I'm getting this error. If I change params[:team_id] for params['team_id'] the error occurs but with: no implicit conversion of String into Integer.

6
  • code should be written in english Commented Jun 4, 2014 at 12:37
  • Dont want to bother you, but its part of conventions to write code in english. Up to you though Commented Jun 4, 2014 at 12:49
  • I agree, sorry for my mistake. Commented Jun 4, 2014 at 12:51
  • @SérgioToledo - You wrote My model: - is it active record model? How do you use teams method in your controller? Commented Jun 4, 2014 at 12:52
  • Yes, it is: class Classification < ActiveRecord::Base Commented Jun 4, 2014 at 13:04

1 Answer 1

3

Your problem lies in a default value for params, it should be hash, not an array:

def self.teams(params = {})
Sign up to request clarification or add additional context in comments.

4 Comments

in fact we got some progress. the test above is passing with your tip (tanks @BroiSatse), but I still got the same error when I pass a parameter: expect(Classification.teams 1).to be_true
@SérgioToledo - You need to decide what you want to pass to this method. Your test is passing an integer, while you set your default value to be an array (or hash). Are you using params only to get team_id value? (BTW, you know you have no access to params in your model without interacting with controller, right?)
@SérgioToledo - According to your comment under the question, you need to change your test to expect(Classification.teams(team_id: 1)).to be_true. When you done please post both your model and controller to codereview.stackexchange.com, as it seems that your code tries to break out of MVC pattern.
Dear @BroiSatse, you're the guy! All issues are solved. Thank you for your help and regards.

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.