Skip to content

Commit

Permalink
Split up specs for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
joshfrench committed Apr 19, 2012
1 parent 0226085 commit b852907
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 256 deletions.
26 changes: 26 additions & 0 deletions spec/models/block_params_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,26 @@
require File.dirname(__FILE__) + '/../spec_helper'

PROC = proc { author.reverse }

class BlockAkismetModel
include Rakismet::Model
rakismet_attrs :author => PROC
end

describe BlockAkismetModel do

before do
@block = BlockAkismetModel.new
comment_attrs.each_pair { |k,v| @block.stub!(k).and_return(v) }
end

it "should accept a block" do
puts BlockAkismetModel.akismet_attrs.inspect
BlockAkismetModel.akismet_attrs[:comment_author].should eql(PROC)
end

it "should eval block with self = instance" do
data = @block.send(:akismet_data)
data[:comment_author].should eql(comment_attrs[:author].reverse)
end
end
19 changes: 19 additions & 0 deletions spec/models/custom_params_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
require File.dirname(__FILE__) + '/../spec_helper'

MAPPED_PARAMS = { :comment_type => :type2, :author => :author2, :content => :content2,
:author_email => :author_email2, :author_url => :author_url2 }

class CustomAkismetModel
include Rakismet::Model
rakismet_attrs MAPPED_PARAMS.dup
end


describe CustomAkismetModel do
it "should override default mappings" do
[:comment_type, :author, :author_url, :author_email, :content].each do |field|
fieldname = field.to_s =~ %r(^comment_) ? field : "comment_#{field}".intern
CustomAkismetModel.akismet_attrs[fieldname].should eql(MAPPED_PARAMS[field])
end
end
end
16 changes: 16 additions & 0 deletions spec/models/extended_params_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,16 @@
require File.dirname(__FILE__) + '/../spec_helper'

EXTRA = { :extra => :extra, :another => lambda { } }

class ExtendedAkismetModel
include Rakismet::Model
rakismet_attrs EXTRA.dup
end

describe ExtendedAkismetModel do
it "should include additional attributes" do
[:extra, :another].each do |field|
ExtendedAkismetModel.akismet_attrs[field].should eql(EXTRA[field])
end
end
end
98 changes: 98 additions & 0 deletions spec/models/rakismet_model_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,98 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe AkismetModel do

before do
@model = AkismetModel.new
comment_attrs.each_pair { |k,v| @model.stub!(k).and_return(v) }
end

it "should have default mappings" do
[:comment_type, :author, :author_email, :author_url, :content].each do |field|
fieldname = field.to_s =~ %r(^comment_) ? field : "comment_#{field}".intern
AkismetModel.akismet_attrs[fieldname].should eql(field)
end
end

it "should have request mappings" do
[:user_ip, :user_agent, :referrer].each do |field|
AkismetModel.akismet_attrs[field].should eql(field)
end
end

it "should populate comment type" do
@model.send(:akismet_data)[:comment_type].should == comment_attrs[:comment_type]
end

describe ".spam?" do

it "should use request variables from Rakismet.request if absent in model" do
[:user_ip, :user_agent, :referrer].each do |field|
@model.should_not respond_to(:field)
end
Rakismet.stub!(:request).and_return(request)
Rakismet.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.should_receive(:akismet_call).once
@model.spam?
@model.spam?
end

it "should be true if comment is spam" do
Rakismet.stub!(:akismet_call).and_return('true')
@model.should be_spam
end

it "should be false if comment is not spam" do
Rakismet.stub!(:akismet_call).and_return('false')
@model.should_not be_spam
end

it "should set akismet_response" do
Rakismet.stub!(:akismet_call).and_return('response')
@model.spam?
@model.akismet_response.should eql('response')
end

it "should not throw an error if request vars are missing" do
Rakismet.stub!(:request).and_return(empty_request)
lambda { @model.spam? }.should_not raise_error(NoMethodError)
end
end


describe ".spam!" do
it "should call Base.akismet_call with submit-spam" do
Rakismet.should_receive(:akismet_call).with('submit-spam', akismet_attrs)
@model.spam!
end

it "should mutate #spam?" do
Rakismet.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.should_receive(:akismet_call).with('submit-ham', akismet_attrs)
@model.ham!
end

it "should mutate #spam?" do
Rakismet.stub!(:akismet_call)
@model.instance_variable_set(:@_spam, true)
@model.ham!
@model.should_not be_spam
end
end

end
23 changes: 23 additions & 0 deletions spec/models/request_params_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.dirname(__FILE__) + '/../spec_helper'

class RequestParams
include Rakismet::Model
attr_accessor :user_ip, :user_agent, :referrer
end

describe RequestParams do
before do
@model = RequestParams.new
attrs = comment_attrs(:user_ip => '192.168.0.1', :user_agent => 'Rakismet', :referrer => 'http://localhost/referrer')
attrs.each_pair { |k,v| @model.stub!(k).and_return(v) }
end

it "should use local values even if Rakismet.request is populated" do
Rakismet.stub(:request).and_return(request)
Rakismet.should_receive(:akismet_call).
with('comment-check', akismet_attrs.merge(:user_ip => '192.168.0.1',
:user_agent => 'Rakismet',
:referrer => 'http://localhost/referrer'))
@model.spam?
end
end
Loading

0 comments on commit b852907

Please sign in to comment.