Permalink
Browse files

Merge pull request #163 from takai/rspec_modernization

Modernize specs
  • Loading branch information...
2 parents ebd1529 + ff3f31e commit 0a470c6099773398842dafa4df0f80d9df6e4e44 @ayumin ayumin committed Feb 18, 2012
Showing with 85 additions and 108 deletions.
  1. +1 −0 Gemfile
  2. +2 −0 Gemfile.lock
  3. +15 −0 spec/spec_helper.rb
  4. +3 −11 spec/unit/category_spec.rb
  5. +1 −1 spec/unit/page_spec.rb
  6. +13 −20 spec/unit/post_spec.rb
  7. +1 −3 spec/unit/snippet_spec.rb
  8. +5 −8 spec/unit/tag_spec.rb
  9. +44 −65 spec/unit/user_spec.rb
View
@@ -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
View
@@ -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)
@@ -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)
View
@@ -20,6 +20,7 @@
require 'rack/test'
require 'rspec'
require 'factory_girl'
+require 'database_cleaner'
require 'factories'
@@ -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
@@ -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
@@ -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
@@ -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') }
@@ -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
@@ -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
@@ -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
View
@@ -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
View
@@ -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.