【发布时间】:2015-11-18 14:30:02
【问题描述】:
假设我们有一个这样的 ruby 类:
class Song
attr_reader :id, :title, :author, :path
def initialize(id, title, author, path)
@id = id
@title= title
@author= author
@path = path
end
def change_title(new)
@title = new
end
def change_path(new)
@path = new
end
# other functions here, some operations and stuff
end
如果我们要将它“转换”为 Rails ActiveRecord,我们应该怎么做? (请举例)我对此很陌生,不太了解它是如何工作的,所以我想我可以在这里问。
第二部分是,我们有很多测试(例如 RSpec)
before(:each){
@song = Song.new(Random.rand(100),
'Some song', 'Some author', 'C:/song.mp3')
}
it 'change songs path' do
new_path = 'D:/new_path/something.mp3'
@song.change_path(new_path)
expect(@song.path).to eq new_path
end
您能否提供一些示例,说明我们如何为测试生成测试数据以及我们应该如何更改测试,例如使用 Fixtures 或 Factory_girl 等。
对于你们中的一些人来说很愚蠢的问题也许很抱歉,因为有文档,但我觉得很难理解,所以想我提供的实际情况可能会有所帮助。
【问题讨论】:
标签: ruby-on-rails ruby unit-testing activerecord rspec