移行ファイルをテストします。場所とユーザーの2つのテーブルがあります。場所has_manyのユーザー。テーブルはlocation_idによって接続されます。移行では、「場所」表の列「名前」に固有の制約を追加し、「場所」表の重複する場所名を持つ重複する行を削除し、すべてのユーザーにその場所の最初の出現箇所を指すようにします。Rails:移行ファイルのテスト
これは私の移行ファイルの一部です:
def self.remove_duplications
grouped = all.group_by{|location| [location.name] }
grouped.values.each do |duplicates|
# the first one we want to keep right?
first_one = duplicates.shift # or pop for last one
users = User.all
users.each do |user|
if user.location && user.location.name == first_one.name
user.location_id = first_one.id
user.save!
end
end
duplicates.each do |duplicate|
duplicate.destroy!
end
end
end
end
def self.up
Location.remove_duplications
remove_index :locations, column: :name
add_index :locations, :name, unique: true
end
def self.down
remove_index :locations, column: :name # remove unique index
add_index :locations, :name # adds just index, without unique
end
どのように私はこれをテストすることができますか?それは手動でそれをテストするのが難しくなってきた。
http://blog.carbonfive.com/2011/01/27/start-testing-your-migrations-right-now/ – p4sh4
私はこれを見ていましたが、テスト用にRspecを使用していました。しかし、私はRspecを使用していません。その他の提案はありますか? – Abhishek