DEV: Improve specs for validating a unique post.

DEV: Improve specs for validating a unique post.

Follow up to d2a7f29595786376a3010cb7e320d66f5b8d60ef which caused post edits to fail.

diff --git a/spec/components/validators/post_validator_spec.rb b/spec/components/validators/post_validator_spec.rb
index 91f36ab..b09a1bb 100644
--- a/spec/components/validators/post_validator_spec.rb
+++ b/spec/components/validators/post_validator_spec.rb
@@ -2,7 +2,8 @@ require 'rails_helper'
 require_dependency 'validators/post_validator'
 
 describe Validators::PostValidator do
-  let(:post) { build(:post, topic: Fabricate(:topic)) }
+  let(:topic) { Fabricate(:topic) }
+  let(:post) { build(:post, topic: topic) }
   let(:validator) { Validators::PostValidator.new({}) }
 
   context "#post_body_validator" do
@@ -186,35 +187,51 @@ describe Validators::PostValidator do
   end
 
   describe "unique_post_validator" do
+    let(:user) { Fabricate(:user, id: 999) }
+    let(:post) { Fabricate(:post, user: user, topic: topic) }
+
     before do
       SiteSetting.unique_posts_mins = 5
+      post.store_unique_post_key
+      @key = post.unique_post_key
+    end
+
+    after do
+      $redis.del(@key)
     end
 
     context "post is unique" do
-      before do
-        post.stubs(:matches_recent_post?).returns(false)
+      let(:new_post) do
+        Fabricate.build(:post, user: user, raw: "unique content", topic: topic)
       end
 
       it "should not add an error" do
+        validator.unique_post_validator(new_post)
+        expect(new_post.errors.count).to eq(0)
+      end
+
+      it 'should not add an error when changing an existing post' do
+        post.raw = "changing raw"
+
         validator.unique_post_validator(post)
         expect(post.errors.count).to eq(0)
       end
     end
 
     context "post is not unique" do
-      before do
-        post.stubs(:matches_recent_post?).returns(true)
+      let(:new_post) do
+        Fabricate.build(:post, user: user, raw: post.raw, topic: topic)
       end
 
       it "should add an error" do
-        validator.unique_post_validator(post)
-        expect(post.errors.count).to be > 0
+        validator.unique_post_validator(new_post)
+        expect(new_post.errors.keys).to contain_exactly(:raw)
       end
 
       it "should not add an error if post.skip_unique_check is true" do
-        post.skip_unique_check = true
-        validator.unique_post_validator(post)
-        expect(post.errors.count).to eq(0)
+        new_post.skip_unique_check = true
+        validator.unique_post_validator(new_post)
+        expect(new_post.errors.count).to eq(0)
       end
     end
   end

GitHub sha: 1bc96177

1 Like