olduğundan emin olun Yeni programlayıcı burada. Projemde Reddit klonu olan bir öğrenciyim. Şu anda RSPEC'e tanıtıldım. Diğer alıştırmalarda kullanılmak üzere kendi Model testlerimi yazmaya başlamak zorundayım. Söz konusu model oluşturulmadı, bir sonraki ödevde olacak. Birisi bunu doğru yapıp yapmadığımı kontrol edebilir mi?İlk RSPEC testi, değerin 1 veya -1
In the next checkpoint, we'll add a Vote model. This model will feature an inclusion validation. Inclusion validation ensures that a vote's value attribute is either 1 or -1. If a vote is initialized with any other value, it will not save.
- Create VoteSpec:
Spec/modeller/vote_spec.rb
describe Vote do
describe "validations" do
describe "value validation" do
it "only allows -1 or 1 as values" do
# your expectations here
end
end
end
end
Write a spec that asserts the validations work as expected.
Use RSpec's expect().to eq() syntax. As you may recall from the specs in the Ruby exercises, you can assert that something should equal false or true.
You won't be able to run the tests because we haven't generated the model we're testing.
Aşağıda benim uygulamasıdır:
describe Vote do
describe "validations" do
before do
2.times { @vote.create(value: 1) }
3.times { @vote.create(value: -1) }
2.times { @vote.create(value: 3) }
end
describe "value validation" do
it "only allows -1 or 1 as values" do
expect (@vote.value).to eq(-1)
end
it "only allows -1 or 1 as values" do
expect (@vote.value).to eq(1)
end
end
end
end
Saygılarımızla.
Düzenleme:
describe Vote do
describe "validations" do
before do
2.times { Vote.create(value: 1) }
3.times { Vote.create(value: 0) }
2.times { Vote.create(value: 3) }
end
describe "value validation" do
it "only allows -1 as value" do
expect (@vote.value).to eq(-1)
end
it "only allows 1 as value" do
expect (@vote.value).to eq(1)
end
it "it prohibits other values" do
expect(@vote.value).to_not be_valid
end
end
end
end
Ben de ilk başta çalıştı bu kod ile çalıştı ama şimdi bir sonraki atama başarısız olması: Bu biraz özel durumda
require 'rails_helper'
describe Vote do
describe "value validation" do
it "allows -1" do
value = Vote.create(value: -1)
expect(value).to be_valid
end
it "allows +1" do
value = Vote.create(value: +1)
expect(value).to be_valid
end
it "prohibits other values" do
value = Vote.create(value: 0)
expect(value).to_not be_valid
end
end
end
▶ rspec spec
...FFF
Failures:
1) Vote value validation allows -1
Failure/Error: value = Vote.create(value: -1)
NoMethodError:
undefined method `update_rank' for nil:NilClass
# ./app/models/vote.rb:12:in `update_post'
# ./spec/models/vote_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Vote value validation allows +1
Failure/Error: value = Vote.create(value: +1)
NoMethodError:
undefined method `update_rank' for nil:NilClass
# ./app/models/vote.rb:12:in `update_post'
# ./spec/models/vote_spec.rb:12:in `block (3 levels) in <top (required)>'
3) Vote value validation prohibits other values
Failure/Error: expect(value).to eq(false)
expected: false
got: #<Vote id: nil, value: 0, user_id: nil, post_id: nil, created_at: nil, updated_at: nil>
(compared using ==)
# ./spec/models/vote_spec.rb:18:in `block (3 levels) in <top (required)>'
Finished in 0.30485 seconds (files took 3.28 seconds to load)
6 examples, 3 failures
Failed examples:
rspec ./spec/models/vote_spec.rb:6 # Vote value validation allows -1
rspec ./spec/models/vote_spec.rb:11 # Vote value validation allows +1
rspec ./spec/models/vote_spec.rb:16 # Vote value validation prohibits other values
'@ vote.create' başarısız olacak @ oyu yok. Sanırım sen oy ver demek istiyorsun. –
Teşekkürler @AndyWaite Bu mantıklı. Bunun dışında testim iyi görünüyor mu? –
Diğer bazı sorunlar var. Örneğin, "beklediğiniz" ifadeleriniz aynı sonucu kontrol ediyor, ancak her durumda farklı değerler bekliyorsunuz. –