Skip to content

Commit

Permalink
Merge pull request lokka#163 from takai/rspec_modernization
Browse files Browse the repository at this point in the history
Modernize specs
  • Loading branch information
ayumin committed Feb 18, 2012
2 parents ebd1529 + ff3f31e commit 0a470c6
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 108 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -48,6 +48,7 @@ group :test do
gem 'rspec', '~> 2.5'
gem 'simplecov', :require => false if RUBY_VERSION >= '1.9'
gem 'factory_girl'
gem 'database_cleaner'
end

group :mysql do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -31,6 +31,7 @@ GEM
bond (0.4.1)
builder (3.0.0)
coderay (1.0.5)
database_cleaner (0.7.1)
diff-lcs (1.1.3)
dm-aggregates (1.2.0)
dm-core (~> 1.2.0)
Expand Down Expand Up @@ -167,6 +168,7 @@ DEPENDENCIES
bundler
coderay
data_objects!
database_cleaner
dm-aggregates (= 1.2.0)
dm-core (= 1.2.0)
dm-is-searchable (= 1.2.0)
Expand Down
15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -20,6 +20,7 @@
require 'rack/test'
require 'rspec'
require 'factory_girl'
require 'database_cleaner'

require 'factories'

Expand All @@ -38,4 +39,18 @@ def app
config.include Rack::Test::Methods
config.include LokkaTestMethods
config.include Lokka::Helpers

config.include FactoryGirl::Syntax::Methods

config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end
end
14 changes: 3 additions & 11 deletions spec/unit/category_spec.rb
@@ -1,23 +1,15 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Category do
after do
Category.destroy
end
subject { Category }

context "on creating a new category with blank slug" do
before do
Category.new({"title"=>"foo", "description"=>"bar", "slug"=>""}).save
end
subject { Category }
before { create :category }
it { should have(1).item }
end

context "on creating 2 new categories with blank slug" do
before do
Category.new({"title"=>"foo", "description"=>"bar", "slug"=>""}).save
Category.new({"title"=>"bar", "description"=>"buz", "slug"=>""}).save
end
before { create_list :category, 2 }
subject { Category }
it { should have(2).item }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/page_spec.rb
Expand Up @@ -2,6 +2,6 @@

describe Page do
describe '.first' do
it { lambda { Page.first }.should_not raise_error }
it { expect { Page.first }.not_to raise_error }
end
end
33 changes: 13 additions & 20 deletions spec/unit/post_spec.rb
@@ -1,13 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Post do
after do
Post.destroy
User.destroy # User is generated for association
end

context 'with slug' do
subject { Factory(:post_with_slug) }
subject { build :post_with_slug }

its(:link) { should eq('/welcome-lokka') }

Expand All @@ -31,7 +26,7 @@
end

context "with id 1" do
subject { Factory(:post, :id => 1) }
subject { build :post, :id => 1 }
its(:edit_link) { should eq('/admin/posts/1/edit') }
end

Expand All @@ -45,40 +40,38 @@
end

context 'default' do
let(:post) { Factory(:post) }
let(:post) { build :post }
it { post.body.should == post.raw_body }
end
end

context "previous or next" do
before do
@before = Factory(:xmas_post)
@after = Factory(:newyear_post)
end
let!(:before) { create :xmas_post }
let!(:after) { create :newyear_post }

it "should return previous page instance" do
@after.prev.should == @before
@after.prev.created_at.should < @after.created_at
after.prev.should == before
after.prev.created_at.should < after.created_at
end

it "should return next page instance" do
@before.next.should == @after
@before.next.created_at.should > @before.created_at
before.next.should == after
before.next.created_at.should > before.created_at
end

describe "the latest article" do
subject { @after }
subject { after }
its(:next) { should be_nil }
end

describe "the first article" do
subject { @before }
subject { before }
its(:prev) { should be_nil }
end
end

describe '.first' do
before { Factory(:post) }
it { lambda { Post.first }.should_not raise_error }
before { build :post }
it { expect { Post.first }.not_to raise_error }
end
end
4 changes: 1 addition & 3 deletions spec/unit/snippet_spec.rb
@@ -1,10 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "Snippet" do
after { Snippet.destroy }

context 'with id 1' do
subject { Factory(:snippet, :id => 1) }
subject { build :snippet, :id => 1 }
its(:edit_link) { should eq('/admin/snippets/1/edit') }
end
end
13 changes: 5 additions & 8 deletions spec/unit/tag_spec.rb
@@ -1,17 +1,14 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe 'Tag' do
after do
Tag.destroy
Tagging.destroy
end

context "with name lokka" do
before { @tag = Factory(:tag, :name => 'lokka') }
subject { @tag }
let!(:tag) { create :tag, :name => 'lokka' }
subject { tag }

its(:link) { should == '/tags/lokka/' }

it 'Tag(name) should return the instance' do
Tag('lokka').should eql(@tag)
Tag('lokka').should eql(tag)
end
end
end
109 changes: 44 additions & 65 deletions spec/unit/user_spec.rb
@@ -1,82 +1,61 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "User" do
after { User.destroy }
describe User do
subject { user }

context "register" do
before do
@user = User.new(
:name => 'Johnny',
:email => 'johnny@example.com',
:password => 'password',
:password_confirmation => 'password'
)
end
it "should be able to register a user" do
@user.save.should be_true
@user.name.should eq('Johnny')
@user.email.should eq('johnny@example.com')
end
it "should not be able to register a user when name is blank" do
@user.name = ''
@user.save.should_not be_true
end
it "should not be able to register a user when email is blank" do
@user.email = ''
@user.save.should_not be_true
shared_examples 'user with validation' do
it 'saves successfully' do
subject.save.should be_true
end

describe 'strip spaces' do
it "should strip anteroposterior spaces" do
@user.name = ' Johnny '
@user.save.should be_true
@user.name.should eq('Johnny')
end
it "should not strip middle spaces" do
@user.name = ' Johnny Depp '
@user.save.should be_true
@user.name.should eq('Johnny Depp')
context 'with blank name' do
before { user.name = '' }

it 'fails to save' do
subject.save.should_not be_true
end
end
end

context "update" do
before { @user = Factory(:user, :name => 'Johnny') }
it "should be updated" do
@user.name = 'Jack'
@user.save.should be_true
@user.name.should eq('Jack')
end
it "should not be updated when name is blank" do
@user.name = ''
@user.save.should_not be_true
end
it "should not be updated when email is blank" do
@user.email = ''
@user.save.should_not be_true
context 'with blank email' do
before { user.email = '' }

it 'fails to save' do
subject.save.should_not be_true
end
end

describe 'strip spaces' do
it "should strip anteroposterior spaces" do
@user.name = ' Jack '
@user.save.should be_true
@user.name.should eq('Jack')
context 'with trailing whitespaces' do
before do
user.name = ' Johnny Depp '
end
it "should not strip middle spaces" do
@user.name = ' Jack Sparrow '
@user.save.should be_true
@user.name.should eq('Jack Sparrow')

it 'trims whitespace after save' do
subject.save
subject.name.should eq 'Johnny Depp'
end
end
end

context "guest" do
before { @guest = GuestUser.new }
it "not admin" do
@guest.admin?.should be_false
end
it "permission leve 0" do
@guest.permission_level.should eq(0)
end
describe '#save' do
let(:user) do
User.new(:name => 'Johnny',
:email => 'johnny@example.com',
:password => 'password',
:password_confirmation => 'password')
end
it_behaves_like 'user with validation'
end

describe "#update" do
let!(:user) { create :user, :name => 'Johnny' }

it_behaves_like 'user with validation'
end

end

describe GuestUser do
it { should_not be_admin }
its(:permission_level) { should eq 0 }
end

0 comments on commit 0a470c6

Please sign in to comment.