完璧なテストには、テスト フレームワークとテスト対象のクラスが含まれているだけだと言われています。他のすべては嘲笑されるべきです。では、協会はどうでしょうか。
has_many
そして、単純な関連付けではなく、belongs_to
関連付けの拡張とスコープを意味します。スコープの仕様を書きたいのですが、どうすればいいのかわかりません。
完璧なテストには、テスト フレームワークとテスト対象のクラスが含まれているだけだと言われています。他のすべては嘲笑されるべきです。では、協会はどうでしょうか。
has_many
そして、単純な関連付けではなく、belongs_to
関連付けの拡張とスコープを意味します。スコープの仕様を書きたいのですが、どうすればいいのかわかりません。
私もこれに引っかかりました。Rspec では、"単体テスト" の概念が取り除かれています。実際、Rails での単体テストは、少なくとも私にとっては、モデルの属性値をテストすることを意味します。しかし、その通りです。協会についてはどうでしょうか。
Rspec では、spec/models
ディレクトリを作成し、モデルをテストするだけです。モデル スペック ( spec/models/user_spec.rb
) の上部には、単体テスト (属性のテスト) があり、以下の各関連付けをテストします。
require 'spec_helper'
describe User do
context ":name" do
it "should have a first name"
it "should have a last name"
end
# all tests related to the gender attribute
context ":gender" do
it "should validate gender"
end
# all tests related to "belongs_to :location"
context ":location" do
it "should :belong_to a location"
it "should validate the location"
end
# all tests related to "has_many :posts"
context ":posts" do
it "should be able to create a post"
it "should be able to create several posts"
it "should be able to list most recent posts"
end
end
しかし、今、テストでPost
とLocation
モデルをUser
テストしていますか? うん。しかし、Post
モデルには、ユーザーとの関係で行うこと以外にも、たくさんの余分なものがあります。と同じLocation
。したがって、次のspec/models/location_spec.rb
ようになります。
require 'spec_helper'
describe Location do
context ":city" do
it "should have a valid city"
end
context ":geo" do
it "should validate geo coordinates"
end
end
私の意見では、そのどれもからかわれるべきではありません。ある時点で、関連付けが保存され、クエリ可能であることを実際にテストする必要があります。それがここです。モデル仕様では、属性の「単体テスト」と関連付けの「統合テスト」があると考えてください。