如果启用了新消息,则会自动从之前的新闻中删除已启用的消息。 我写了一个规范,但他们失败了。这些规范可以改进吗?
Post.rb
before_validation :removes_enabled
def removes_enabled
if enabled_changed? && enabled?
Post.update_all( enabled: false )
end
end
我的规格:
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
答案 0 :(得分:0)
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