我是一个新手,正在努力通过测试。我有 3 个班级,艺术家、歌曲和流派。我试图通过的测试如下:
test 'A genre has many artists' do
genre = Genre.new.tap{|g| g.name = 'rap'}
[1,2].each do
artist = Artist.new
song = Song.new
song.genre = genre
artist.add_song(song)
end
assert_equal genre.artists.count, 2
end
这是我的艺术家课程,我需要调整 add_song 方法。当一首歌被添加到艺术家时,我试图实例化一个新的流派对象并将艺术家也添加到该流派中。虽然目前不工作,但当我调用genre.artists 时,它返回一个空数组。类艺术家 attr_accessor :name, :songs, :genres, :genre, :artists @@artists = []
def initialize(name = name, genre = genre)
@artists = []
@songs = []
@genre = genre
@genres = []
@name = name
@@artists << self
end
def self.all
@@artists
end
def self.reset_artists
@@artists = []
end
def self.count
self.all.size
end
def songs_count
self.songs.size
end
def count
self.size
end
def add_song(song)
@songs << song
@genres << song.genre
Genre.new(self)
end
end
class Genre
attr_accessor :name, :songs, :artists
@@genres = []
def initialize(artists = artists)
@songs = []
@artists = artists
@name = name
@@genres << self
end
def count
self.artists.count
end
def self.all
@@genres
end
def self.reset_genres
@@genre = []
end
end
class Song
attr_accessor :name, :genre, :artist
def initialize(name = name, artist = artist, genre = genre)
@name = name
@artist = artist
@genre = genre
end
end