@depaの答えは良いですが、代替手段のために、そして私はより短い構文を好むためです:
describe User do
let(:user) { User.create(:email => email, :password => password) }
describe "Authentication" do
subject { User.authenticate(user.email, user.password) }
context "Given an existing user" do
let(:email) { "foo@bar.com" }
context "With a correct password" do
let(:password) { "foo" }
it { should eq(user) }
end
context "With an incorrect password" do
let(:password) { "bar" }
it { should be_nil }
end
end
end
end
sytax に対する私の好みは別として、これには他のスタイルに比べて 2 つの大きな利点があると思います。
- 特定の値を簡単にオーバーライドできます (
password
上記で行ったように) 。
- さらに重要なことは、空白のパスワード、存在しないユーザーなど、テストされていないものを強調表示することです。
そういうわけで、ととの組み合わせはcontext
、私にとって、通常のスタイルよりもはるかに優れています。subject
let