describe "#remove_enabled" do
let!(:post) { create :post, enabled: true }
it "should remove enabled for other post" do
create :post, enabled: true
expect(post.reload.enabled).to eq(false)
end
it "should not remove enabled for other post if current post is not enabled" do
create :post, enabled: false
expect(post.reload.enabled).to eq(true)
end
end
Specs for the method of the model DRY
-
10-10-2022 - |
Question
If new news is enabled, it automatically removes enabled from the previous news. I wrote a spec, but they fail. As these spec can be improved?
Post.rb
before_validation :removes_enabled
def removes_enabled
if enabled_changed? && enabled?
Post.update_all( enabled: false )
end
end
My spec:
describe "#remove_enabled" do
let!(:p1) {create :post, enabled: true }
it "should remove enabled for other post" do
p2 = create :post, enabled: true
expect(p1.enabled).to eq(false)
end
it "should not remove enabled for other post if current post is not enabled" do
p2 = create :post, enabled: false
expect(p1.enabled).to eq(true)
end
end
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow