Refactoring of specs

* split the one large spec file in multiple small, one for each model
* helper for getting dump object in tests
This commit is contained in:
Marc Remolt 2011-06-05 13:09:06 +02:00
parent e1c60163e4
commit ddb65040a7
8 changed files with 264 additions and 220 deletions

View File

@ -1,3 +1,6 @@
require 'yaml'
YAML::ENGINE.yamler= 'syck'
require 'rubygems'
gemfile = File.expand_path('../../../../Gemfile', __FILE__)

View File

@ -0,0 +1,28 @@
require 'spec_helper'
describe Refinery::WordPress::Author, :type => :model do
let(:author) { test_dump.authors.first }
it { author.login.should == 'admin' }
it { author.email.should == 'admin@example.com' }
describe "#to_refinery" do
before do
@user = author.to_refinery
end
it "should create a User object" do
User.should have(1).record
@user.should be_a(User)
end
it "the @user should be persisted" do
@user.should be_persisted
end
it "should have copied the attributes from Refinery::WordPress::Author" do
author.login.should == @user.username
author.email.should == @user.email
end
end
end

View File

@ -0,0 +1,29 @@
require 'spec_helper'
describe Refinery::WordPress::Category, :type => :model do
let(:category) { Refinery::WordPress::Category.new('Rant') }
describe "#name" do
specify { category.name.should == 'Rant' }
end
describe "#==" do
specify { category.should == Refinery::WordPress::Category.new('Rant') }
specify { category.should_not == Refinery::WordPress::Category.new('Tutorials') }
end
describe "#to_refinery" do
before do
@category = category.to_refinery
end
it "should create a BlogCategory" do
BlogCategory.should have(1).record
end
it "should copy the name over to the BlogCategory object" do
@category.title.should == category.name
end
end
end

View File

@ -18,29 +18,13 @@ describe Refinery::WordPress::Dump, :type => :model do
Refinery::WordPress::Tag.new('php'), Refinery::WordPress::Tag.new('ruby')]
end
specify { dump.tags.count == 4 }
it "should return all included tags" do
tags.each do |tag|
dump.tags.should include(tag)
end
end
context "the last tag" do
let(:tag) { dump.tags.last }
describe "#to_refinery" do
before do
@tag = tag.to_refinery
end
it "should create a ActsAsTaggableOn::Tag" do
::ActsAsTaggableOn::Tag.should have(1).record
end
it "should copy the name over to the Tag object" do
@tag.name.should == tag.name
end
end
end
end
describe "#categories" do
@ -49,230 +33,30 @@ describe Refinery::WordPress::Dump, :type => :model do
Refinery::WordPress::Category.new('Uncategorized') ]
end
specify { dump.categories.count == 4 }
it "should return all included categories" do
categories.each do |cat|
dump.categories.should include(cat)
end
end
context "the last category" do
let(:category) { dump.categories.last }
describe "#to_refinery" do
before do
@category = category.to_refinery
end
it "should create a BlogCategory" do
BlogCategory.should have(1).record
end
it "should copy the name over to the BlogCategory object" do
@category.title.should == category.name
end
end
end
end
describe "#pages" do
it "should return all included pages" do
dump.pages.should have(3).pages
end
context "the About me page" do
let(:page) { dump.pages.last }
it { page.title.should == 'About me' }
it { page.content.should include('Lorem ipsum dolor sit') }
it { page.creator.should == 'admin' }
it { page.post_date.should == DateTime.new(2011, 5, 21, 12, 25, 42) }
it { page.post_id.should == 10 }
it { page.parent_id.should == 8 }
it { page.should == dump.pages.last }
it { page.should_not == dump.pages.first }
describe "#to_refinery" do
include ::ActionView::Helpers::TagHelper
include ::ActionView::Helpers::TextHelper
before do
# "About me" has a parent page with id 8 in the XML dump,
# would otherwise fails creation
Page.create! :id => 8, :title => 'About'
@count = Page.count
@page = page.to_refinery
end
it "should create a Page object" do
Page.should have(@count + 1).record
end
it "should copy the attributes from Refinery::WordPress::Page" do
@page.title.should == page.title
@page.draft.should == page.draft?
@page.created_at.should == page.post_date
@page.parts.first.body.should == "#{simple_format(page.content)}"
end
end
end
end
describe "#authors" do
it "should return all authors" do
dump.authors.should have(1).author
end
context "the first author" do
let(:author) { dump.authors.first }
it { author.login.should == 'admin' }
it { author.email.should == 'admin@example.com' }
describe "#to_refinery" do
before do
@user = author.to_refinery
end
it "should create a User object" do
User.should have(1).record
@user.should be_a(User)
end
it "the @user should be persisted" do
@user.should be_persisted
end
it "should have copied the attributes from Refinery::WordPress::Author" do
author.login.should == @user.username
author.email.should == @user.email
end
end
end
end
describe "#posts" do
it "should return all posts" do
dump.posts.should have(3).posts
end
context "the last post" do
let(:post) { dump.posts.last }
it { post.title.should == 'Third blog post' }
it { post.content.should include('Lorem ipsum dolor sit') }
it { post.creator.should == 'admin' }
it { post.post_date.should == DateTime.new(2011, 5, 21, 12, 24, 45) }
it { post.post_id.should == 6 }
it { post.parent_id.should == 0 }
it { post.status.should == 'publish' }
it { post.should == dump.posts.last }
it { post.should_not == dump.posts.first }
describe "#categories" do
it { post.categories.should have(1).category }
it { post.categories.first.should == Refinery::WordPress::Category.new('Rant') }
end
describe "#tags" do
it { post.tags.should have(3).tags }
it { post.tags.should include(Refinery::WordPress::Tag.new('css')) }
it { post.tags.should include(Refinery::WordPress::Tag.new('html')) }
it { post.tags.should include(Refinery::WordPress::Tag.new('php')) }
end
it { post.tag_list.should == 'css,html,php' }
describe "#comments" do
it "should return all attached comments" do
post.comments.should have(2).comments
end
context "the last comment" do
let(:comment) { post.comments.last }
it { comment.author.should == 'admin' }
it { comment.email.should == 'admin@example.com' }
it { comment.url.should == 'http://www.example.com/' }
it { comment.date.should == DateTime.new(2011, 5, 21, 12, 26, 30) }
it { comment.content.should include('Another one!') }
it { comment.should be_approved }
it { comment.should == post.comments.last }
describe "#to_refinery" do
before do
@comment = comment.to_refinery
end
it "should not save the comment, only initialize it" do
BlogComment.should have(0).records
@comment.should be_new_record
end
it "should copy the attributes from Refinery::WordPress::Comment" do
@comment.name.should == comment.author
@comment.email.should == comment.email
@comment.body.should == comment.content
@comment.state.should == 'approved'
@comment.created_at.should == comment.date
end
end
end
end
describe "#to_refinery" do
before do
@user = User.create! :username => 'admin', :email => 'admin@example.com',
:password => 'password', :password_confirmation => 'password'
end
context "with a unique title" do
before do
@post = post.to_refinery
end
it { BlogPost.should have(1).record }
it "should copy the attributes from Refinery::WordPress::Post" do
@post.title.should == post.title
@post.body.should == post.content_formatted
@post.draft.should == post.draft?
@post.published_at.should == post.post_date
@post.created_at.should == post.post_date
@post.author.username.should == post.creator
end
it "should assign a category for each Refinery::WordPress::Category" do
@post.categories.should have(post.categories.count).records
end
it "should assign a comment for each Refinery::WordPress::Comment" do
@post.comments.should have(post.comments.count).records
end
end
context "with a duplicate title" do
before do
BlogPost.create! :title => post.title, :body => 'Lorem', :author => @user
@post = post.to_refinery
end
it { BlogPost.should have(2).records }
it "should create the BlogPost with #post_id attached" do
@post.title.should == "#{post.title}-#{post.post_id}"
end
end
end
end
end
end

View File

@ -0,0 +1,43 @@
require 'spec_helper'
describe Refinery::WordPress::Page, :type => :model do
let(:dump) { test_dump }
let(:page) { test_dump.pages.last }
it { page.title.should == 'About me' }
it { page.content.should include('Lorem ipsum dolor sit') }
it { page.creator.should == 'admin' }
it { page.post_date.should == DateTime.new(2011, 5, 21, 12, 25, 42) }
it { page.post_id.should == 10 }
it { page.parent_id.should == 8 }
it { page.should == dump.pages.last }
it { page.should_not == dump.pages.first }
describe "#to_refinery" do
include ::ActionView::Helpers::TagHelper
include ::ActionView::Helpers::TextHelper
before do
# "About me" has a parent page with id 8 in the XML dump,
# would otherwise fails creation
Page.create! :id => 8, :title => 'About'
@count = Page.count
@page = page.to_refinery
end
it "should create a Page object" do
Page.should have(@count + 1).record
end
it "should copy the attributes from Refinery::WordPress::Page" do
@page.title.should == page.title
@page.draft.should == page.draft?
@page.created_at.should == page.post_date
@page.parts.first.body.should == "#{simple_format(page.content)}"
end
end
end

View File

@ -0,0 +1,116 @@
require 'spec_helper'
describe Refinery::WordPress::Post, :type => :model do
let(:post) { test_dump.posts.last }
it { post.title.should == 'Third blog post' }
it { post.content.should include('Lorem ipsum dolor sit') }
it { post.content_formatted.should include('Lorem ipsum dolor sit') }
it { post.creator.should == 'admin' }
it { post.post_date.should == DateTime.new(2011, 5, 21, 12, 24, 45) }
it { post.post_id.should == 6 }
it { post.parent_id.should == nil }
it { post.status.should == 'publish' }
it { post.should == test_dump.posts.last }
it { post.should_not == test_dump.posts.first }
describe "#categories" do
it { post.categories.should have(1).category }
it { post.categories.first.should == Refinery::WordPress::Category.new('Rant') }
end
describe "#tags" do
it { post.tags.should have(3).tags }
it { post.tags.should include(Refinery::WordPress::Tag.new('css')) }
it { post.tags.should include(Refinery::WordPress::Tag.new('html')) }
it { post.tags.should include(Refinery::WordPress::Tag.new('php')) }
end
it { post.tag_list.should == 'css,html,php' }
describe "#comments" do
it "should return all attached comments" do
post.comments.should have(2).comments
end
context "the last comment" do
let(:comment) { post.comments.last }
it { comment.author.should == 'admin' }
it { comment.email.should == 'admin@example.com' }
it { comment.url.should == 'http://www.example.com/' }
it { comment.date.should == DateTime.new(2011, 5, 21, 12, 26, 30) }
it { comment.content.should include('Another one!') }
it { comment.should be_approved }
it { comment.should == post.comments.last }
describe "#to_refinery" do
before do
@comment = comment.to_refinery
end
it "should not save the comment, only initialize it" do
BlogComment.should have(0).records
@comment.should be_new_record
end
it "should copy the attributes from Refinery::WordPress::Comment" do
@comment.name.should == comment.author
@comment.email.should == comment.email
@comment.body.should == comment.content
@comment.state.should == 'approved'
@comment.created_at.should == comment.date
end
end
end
end
describe "#to_refinery" do
before do
@user = User.create! :username => 'admin', :email => 'admin@example.com',
:password => 'password', :password_confirmation => 'password'
end
context "with a unique title" do
before do
@post = post.to_refinery
end
it { BlogPost.should have(1).record }
it "should copy the attributes from Refinery::WordPress::Post" do
@post.title.should == post.title
@post.body.should == post.content_formatted
@post.draft.should == post.draft?
@post.published_at.should == post.post_date
@post.created_at.should == post.post_date
@post.author.username.should == post.creator
end
it "should assign a category for each Refinery::WordPress::Category" do
@post.categories.should have(post.categories.count).records
end
it "should assign a comment for each Refinery::WordPress::Comment" do
@post.comments.should have(post.comments.count).records
end
end
context "with a duplicate title" do
before do
BlogPost.create! :title => post.title, :body => 'Lorem', :author => @user
@post = post.to_refinery
end
it { BlogPost.should have(2).records }
it "should create the BlogPost with #post_id attached" do
@post.title.should == "#{post.title}-#{post.post_id}"
end
end
end
end

View File

@ -0,0 +1,30 @@
require 'spec_helper'
describe Refinery::WordPress::Tag, :type => :model do
let(:tag) { Refinery::WordPress::Tag.new('ruby') }
describe "#name" do
specify { tag.name.should == 'ruby' }
end
describe "#==" do
specify { tag.should == Refinery::WordPress::Tag.new('ruby') }
specify { tag.should_not == Refinery::WordPress::Tag.new('php') }
end
describe "#to_refinery" do
before do
@tag = tag.to_refinery
end
it "should create a ActsAsTaggableOn::Tag" do
::ActsAsTaggableOn::Tag.should have(1).record
end
it "should copy the name over to the Tag object" do
@tag.name.should == tag.name
end
end
end

11
spec/support/helpers.rb Normal file
View File

@ -0,0 +1,11 @@
module Refinery::WordPress::SpecHelpers
def test_dump
file_name = File.realpath(File.join(File.dirname(__FILE__), '../fixtures/wordpress_dump.xml'))
Refinery::WordPress::Dump.new(file_name)
end
end
RSpec.configure do |config|
config.include Refinery::WordPress::SpecHelpers
end