spec/models/rakismet_model_spec.rb in rakismet-0.4.0 vs spec/models/rakismet_model_spec.rb in rakismet-0.4.1

- old
+ new

@@ -9,11 +9,11 @@ include Rakismet::Model attr_accessor :user_ip, :user_agent, :referrer end describe AkismetModel do - + before do @model = AkismetModel.new comment_attrs.each_pair { |k,v| @model.stub!(k).and_return(v) } end @@ -123,21 +123,27 @@ end describe ".spam?" do before do - Rakismet::Base.rakismet_binding = request_binding + Rakismet::Base.current_request = request end it "should eval request variables in context of Base.rakismet_binding" do Rakismet::Base.should_receive(:akismet_call). with('comment-check', akismet_attrs.merge(:user_ip => '127.0.0.1', :user_agent => 'RSpec', :referrer => 'http://test.host/referrer')) @model.spam? end + it "should cache result of #spam?" do + Rakismet::Base.should_receive(:akismet_call).once + @model.spam? + @model.spam? + end + it "should be true if comment is spam" do Rakismet::Base.stub!(:akismet_call).and_return('true') @model.should be_spam end @@ -151,18 +157,18 @@ @model.spam? @model.akismet_response.should eql('response') end it "should not throw an error if request vars are missing" do - Rakismet::Base.rakismet_binding = nil_binding + Rakismet::Base.current_request = empty_request lambda { @model.spam? }.should_not raise_error(NoMethodError) end end describe StoredParams do before do - Rakismet::Base.rakismet_binding = nil + Rakismet::Base.current_request = nil @model = StoredParams.new comment_attrs.each_pair { |k,v| @model.stub!(k).and_return(v) } end it "should use local values if Rakismet binding is not present" do @@ -181,17 +187,31 @@ describe ".spam!" do it "should call Base.akismet_call with submit-spam" do Rakismet::Base.should_receive(:akismet_call).with('submit-spam', akismet_attrs) @model.spam! end + + it "should mutate #spam?" do + Rakismet::Base.stub!(:akismet_call) + @model.instance_variable_set(:@_spam, false) + @model.spam! + @model.should be_spam + end end describe ".ham!" do it "should call Base.akismet_call with submit-ham" do Rakismet::Base.should_receive(:akismet_call).with('submit-ham', akismet_attrs) @model.ham! end + + it "should mutate #spam?" do + Rakismet::Base.stub!(:akismet_call) + @model.instance_variable_set(:@_spam, true) + @model.ham! + @model.should_not be_spam + end end private def comment_attrs(attrs={}) @@ -204,20 +224,18 @@ { :comment_type => 'test', :comment_author_email => 'test@test.host', :comment_author => 'Rails test', :comment_author_url => 'test.host', :comment_content => 'comment content' }.merge(attrs) end - def request_binding - request = OpenStruct.new(:remote_ip => '127.0.0.1', - :user_agent => 'RSpec', - :referer => 'http://test.host/referrer') - binding + def request + OpenStruct.new(:remote_ip => '127.0.0.1', + :user_agent => 'RSpec', + :referer => 'http://test.host/referrer') end - def nil_binding - request = OpenStruct.new(:remote_ip => nil, - :user_agent => nil, - :referer => nil) - binding + def empty_request + OpenStruct.new(:remote_ip => nil, + :user_agent => nil, + :referer => nil) end end \ No newline at end of file