Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use parameters in a constructor in a rails model

I trying to learn tdd using RSpec. I took this example from a cheat sheet I found online and am a bit confused as to how I would implement it. To add MovieList.new is automatic but how would I go about adding a parameter when it is already handled with ActiveRecord. And then to add the 'forward' method as well.

describe "forward" do
  it "should jump to a next movie" do
    next_movie = MovieList.new(2).forward
    next_movie.track_number.should == 2
  end
end
like image 276
basheps Avatar asked Nov 24 '25 14:11

basheps


1 Answers

If this is a test for a MovieList class, create a class called MovieList.

Then in your constructor for that class, make sure it takes in a parameter called track_number, in your test that's the 2.

Then create a method called forward to do whatever you need it to do?

Here's a good example of where I'm going with this: http://rspec.info/

This may sound ambiguous, but so was the question.


EDIT: This is a rough idea of how to create a new MovieList class and initialize it with a parameter called track_number.

def MovieList
  attr_accessor :track_number

  def initialize(track_number)
    @track_number = track_number
  end

  # You can define all your class methods below, you 
  # can start with forward.

  def forward
    # do something...
  end 

end
like image 199
DemitryT Avatar answered Nov 27 '25 05:11

DemitryT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!