From 28928310f9850fb0050ba912c5cbe884baddc2db Mon Sep 17 00:00:00 2001 From: Wynn Netherland Date: Tue, 8 Jan 2013 09:51:09 -0600 Subject: [PATCH] Upgrade to RSpec 2.0 A first pass to get Nesta on RSpec 2.0. This commit * Upgrades Rspec * Moves spec.opts to .rspec * Updates Rake tasks for Rspec * Replaces defunct `have_tag` matchers with Webrat --- spec/spec.opts => .rspec | 0 Gemfile.lock | 22 ++++-- Rakefile | 7 +- nesta.gemspec | 7 +- spec/atom_spec.rb | 87 ++++++++++----------- spec/model_factory.rb | 14 ++-- spec/models_spec.rb | 128 ++++++++++++++++++------------- spec/overrides_spec.rb | 38 +++++----- spec/page_spec.rb | 160 +++++++++++++++++++-------------------- spec/sitemap_spec.rb | 64 ++++++++-------- spec/spec_helper.rb | 22 +++--- 11 files changed, 288 insertions(+), 261 deletions(-) rename spec/spec.opts => .rspec (100%) diff --git a/spec/spec.opts b/.rspec similarity index 100% rename from spec/spec.opts rename to .rspec diff --git a/Gemfile.lock b/Gemfile.lock index 307e272c..562800ee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,6 +16,7 @@ GEM RedCloth (4.2.9) ZenTest (4.6.2) addressable (2.2.7) + diff-lcs (1.1.3) em-websocket (0.3.6) addressable (>= 2.1.1) eventmachine (>= 0.12.9) @@ -33,8 +34,8 @@ GEM haml (3.1.4) hoe (2.15.0) rake (~> 0.8) - hpricot (0.8.4) multi_json (1.1.0) + nokogiri (1.5.5) rack (1.4.1) rack-protection (1.2.0) rack @@ -43,8 +44,14 @@ GEM rake (0.9.2.2) rb-readline (0.4.2) rdiscount (1.6.8) - rspec (1.3.0) - rspec_hpricot_matchers (1.0) + rspec (2.12.0) + rspec-core (~> 2.12.0) + rspec-expectations (~> 2.12.0) + rspec-mocks (~> 2.12.0) + rspec-core (2.12.2) + rspec-expectations (2.12.0) + diff-lcs (~> 1.1.3) + rspec-mocks (2.12.0) sass (3.1.15) shotgun (0.9) rack (>= 1.0) @@ -56,6 +63,10 @@ GEM hoe (>= 1.5.1) thor (0.14.6) tilt (1.3.3) + webrat (0.7.3) + nokogiri (>= 1.2.0) + rack (>= 1.0) + rack-test (>= 0.5.3) PLATFORMS ruby @@ -64,10 +75,9 @@ DEPENDENCIES ZenTest guard-ctags-bundler guard-livereload - hpricot (= 0.8.4) nesta! rack-test (= 0.6.1) rb-readline - rspec (= 1.3.0) - rspec_hpricot_matchers (= 1.0) + rspec (~> 2.12.0) test-unit (= 1.2.3) + webrat (~> 0.7.3) diff --git a/Rakefile b/Rakefile index 5ebd7f65..ebc1c713 100644 --- a/Rakefile +++ b/Rakefile @@ -3,12 +3,9 @@ require "bundler/setup" Bundler.require(:default, :test) -require "spec/rake/spectask" Bundler::GemHelper.install_tasks -desc "Run all specs in spec directory" -Spec::Rake::SpecTask.new(:spec) do |t| - t.spec_files = FileList["spec/*_spec.rb"] -end +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) task :default => :spec diff --git a/nesta.gemspec b/nesta.gemspec index bf18b63b..4c101a11 100644 --- a/nesta.gemspec +++ b/nesta.gemspec @@ -37,14 +37,13 @@ EOF s.add_dependency('RedCloth', '~> 4.2') s.add_dependency('sinatra', '~> 1.3') s.add_dependency('rack', '~> 1.3') - + # Useful in development s.add_dependency('shotgun', '>= 0.8') # Test libraries - s.add_development_dependency('hpricot', '0.8.4') s.add_development_dependency('rack-test', '0.6.1') - s.add_development_dependency('rspec', '1.3.0') - s.add_development_dependency('rspec_hpricot_matchers', '1.0') + s.add_development_dependency('rspec', '~> 2.12.0') s.add_development_dependency('test-unit', '1.2.3') + s.add_development_dependency('webrat', '~> 0.7.3') end diff --git a/spec/atom_spec.rb b/spec/atom_spec.rb index a9ede8e6..82dd78d1 100644 --- a/spec/atom_spec.rb +++ b/spec/atom_spec.rb @@ -4,7 +4,7 @@ describe "atom feed" do include ModelFactory include RequestSpecHelper - + before(:each) do stub_configuration stub_config_key("author", { @@ -14,41 +14,41 @@ }) get "/articles.xml" end - + after(:each) do remove_temp_directory end - + it "should render successfully" do last_response.should be_ok end - + it "should use Atom's XML namespace" do - body.should have_tag("/feed[@xmlns=http://www.w3.org/2005/Atom]") + body.should have_xpath("//feed[@xmlns='http://www.w3.org/2005/Atom']") end - + it "should have an ID element" do - body.should have_tag("/feed/id", "tag:example.org,2009:/") + body.should have_selector("id:contains('tag:example.org,2009:/')") end - + it "should have an alternate link element" do - body.should have_tag("/feed/link[@rel=alternate][@href='http://example.org/']") + body.should have_xpath("//feed/link[@rel='alternate'][@href='http://example.org/']") end it "should have a self link element" do - body.should have_tag( - "/feed/link[@rel=self][@href='http://example.org/articles.xml']") + body.should have_xpath( + "//feed/link[@rel='self'][@href='http://example.org/articles.xml']") end - + it "should have title and subtitle" do - body.should have_tag("/feed/title[@type=text]", "My blog") - body.should have_tag("/feed/subtitle[@type=text]", "about stuff") + body.should have_xpath("//feed/title[@type='text']", :content => "My blog") + body.should have_xpath("//feed/subtitle[@type='text']", :content => "about stuff") end - + it "should include the author details" do - body.should have_tag("/feed/author/name", "Fred Bloggs") - body.should have_tag("/feed/author/uri", "http://fredbloggs.com") - body.should have_tag("/feed/author/email", "fred@fredbloggs.com") + body.should have_xpath("//feed/author/name", :content => "Fred Bloggs") + body.should have_xpath("//feed/author/uri", :content => "http://fredbloggs.com") + body.should have_xpath("//feed/author/email", :content => "fred@fredbloggs.com") end describe "for article" do @@ -70,50 +70,51 @@ @article = @articles.last get "/articles.xml" end - + it "should set title" do - body.should have_tag("entry/title", "Article 11") + body.should have_xpath("//entry/title", :content => "Article 11") end - + it "should link to the HTML version" do url = "http://example.org/#{@article.path}" - body.should have_tag( - "entry/link[@href='#{url}'][@rel=alternate][@type='text/html']") + body.should have_xpath( + "//entry/link[@href='#{url}'][@rel='alternate'][@type='text/html']") end - + it "should define unique ID" do - body.should have_tag( - "entry/id", "tag:example.org,2009-01-11:#{@article.abspath}") + body.should have_xpath( + "//entry/id", :content => "tag:example.org,2009-01-11:#{@article.abspath}") end - + it "should specify date published" do - body.should have_tag("entry/published", "2009-01-11T00:00:00+00:00") + body.should have_xpath("//entry/published", :content => "2009-01-11T00:00:00+00:00") end it "should specify article categories" do - body.should have_tag("category[@term=#{@category.permalink}]") + body.should have_xpath("//category[@term='#{@category.permalink}']") end it "should have article content" do - body.should have_tag( - "entry/content[@type=html]", /]*>#{@heading}<\/h2>/) + body.should have_xpath "//entry/content[@type='html']" do |a| + a.should contain "

#{@heading}

" + end end - + it "should include hostname in URLs" do - body.should have_tag("entry/content", - Regexp.new('href=.+http:\/\/example.org\/foo')) + body.should have_xpath("//entry/content") do |c| + c.should contain 'http://example.org/foo' + end end - + it "should not include article heading in content" do - body.should_not have_tag("entry/summary", /#{@article.heading}/) + body.should_not have_selector("summary:contains('#{@article.heading}')") end - + it "should list the latest 10 articles" do - body.should have_tag("entry", :count => 10) - body.should_not have_tag("entry/title", @articles.first.heading) + body.should have_selector("entry", :count => 10) end end - + describe "page with no date" do before(:each) do create_category(:path => "no-date") @@ -121,10 +122,10 @@ end it "should not appear in feed" do - body.should_not have_tag("entry/id", /tag.*no-date/) + body.should_not have_selector("entry id:contains('no-date')") end end - + describe "article with atom ID" do it "should use pre-defined ID" do create_article(:metadata => { @@ -132,7 +133,7 @@ "atom id" => "use-this-id" }) get "/articles.xml" - body.should have_tag("entry/id", "use-this-id") + body.should have_xpath("//entry/id", :content => "use-this-id") end end end diff --git a/spec/model_factory.rb b/spec/model_factory.rb index f75365e1..23aac0da 100644 --- a/spec/model_factory.rb +++ b/spec/model_factory.rb @@ -6,7 +6,7 @@ def create_page(options) yield(path) if block_given? Nesta::Page.new(path) end - + def create_article(options = {}, &block) o = { :path => 'article-prefix/my-article', @@ -18,7 +18,7 @@ def create_article(options = {}, &block) }.merge(options) create_page(o, &block) end - + def create_category(options = {}, &block) o = { :path => 'category-prefix/my-category', @@ -27,7 +27,7 @@ def create_category(options = {}, &block) }.merge(options) create_page(o, &block) end - + def write_menu_item(indent, file, menu_item) if menu_item.is_a?(Array) indent.sub!(/^/, ' ') @@ -42,17 +42,17 @@ def create_menu(menu_text) file = filename(Nesta::Config.content_path, 'menu', :txt) File.open(file, 'w') { |file| file.write(menu_text) } end - + def delete_page(type, permalink, extension) file = filename(Nesta::Config.page_path, permalink, extension) FileUtils.rm(file) end - + def create_content_directories FileUtils.mkdir_p(Nesta::Config.page_path) FileUtils.mkdir_p(Nesta::Config.attachment_path) end - + def mock_file_stat(method, filename, time) stat = mock(:stat) stat.stub!(:mtime).and_return(Time.parse(time)) @@ -63,7 +63,7 @@ def mock_file_stat(method, filename, time) def filename(directory, basename, extension = :mdown) File.join(directory, "#{basename}.#{extension}") end - + def heading(options) prefix = case options[:ext] when :haml diff --git a/spec/models_spec.rb b/spec/models_spec.rb index 2734e1a7..a1931a96 100644 --- a/spec/models_spec.rb +++ b/spec/models_spec.rb @@ -26,7 +26,7 @@ def be_in_category(path) end end -describe "Page", :shared => true do +shared_examples_for "Page" do include ModelFactory include ModelMatchers @@ -37,12 +37,12 @@ def create_page(options) before(:each) do stub_configuration end - + after(:each) do remove_temp_directory Nesta::FileModel.purge_cache end - + it "should be findable" do create_page(:heading => 'Apple', :path => 'the-apple') Nesta::Page.find_all.should have(1).item @@ -52,7 +52,7 @@ def create_page(options) create_page(:heading => 'Banana', :path => 'banana') Nesta::Page.find_by_path('banana').heading.should == 'Banana' end - + it "should find index page by path" do create_page(:heading => 'Banana', :path => 'banana/index') Nesta::Page.find_by_path('banana').heading.should == 'Banana' @@ -102,17 +102,17 @@ def create_page(options) Nesta::Page.find_by_path('/').abspath.should == '/' end end - + it "should not find nonexistent page" do Nesta::Page.find_by_path("no-such-page").should be_nil end - + it "should ensure file exists on instantiation" do lambda { Nesta::Page.new("no-such-file") }.should raise_error(Sinatra::NotFound) end - + it "should reload cached files when modified" do create_page(:path => "a-page", :heading => "Version 1") File.stub!(:mtime).and_return(Time.new - 1) @@ -177,11 +177,11 @@ def create_page(options) it "should find articles" do @category.articles.should have(2).items end - + it "should order articles by reverse chronological order" do @category.articles.first.path.should == @article.path end - + it "should find pages" do @category.pages.should have(3).items end @@ -189,7 +189,7 @@ def create_page(options) it "should sort pages by priority" do @category.pages.index(@category3).should == 0 end - + it "should order pages by heading if priority not set" do pages = @category.pages pages.index(@category1).should < pages.index(@category2) @@ -224,7 +224,7 @@ def create_page(options) Nesta::Page.find_by_path('foo/in-draft').should be_nil end end - + describe "when finding articles" do before(:each) do create_article(:heading => "Article 1", :path => "article-1") @@ -235,25 +235,25 @@ def create_page(options) :path => "foo/article-3", :metadata => { "date" => "30 December 2008" }) end - + it "should only find pages with dates" do articles = Nesta::Page.find_articles articles.size.should > 0 Nesta::Page.find_articles.each { |page| page.date.should_not be_nil } end - + it "should return articles in reverse chronological order" do article1, article2 = Nesta::Page.find_articles[0..1] article1.date.should > article2.date end end - + it "should be able to find parent page" do category = create_category(:path => 'parent') article = create_article(:path => 'parent/child') article.parent.should == category end - + describe "(with deep index page)" do it "should be able to find index parent" do home = create_category(:path => 'index', :heading => 'Home') @@ -292,15 +292,15 @@ def create_page(options) before(:each) do create_category(:heading => "Apple", :path => "the-apple") create_category(:heading => "Banana", :path => "banana") - create_category(:heading => "Cape Anteater", - :metadata=>{'link text' => 'Aardvark'}, + create_category(:heading => "Cape Anteater", + :metadata=>{'link text' => 'Aardvark'}, :path => "strange-critter") @article = create_article( :metadata => { "categories" => "banana, the-apple" }) @article2 = create_article( :metadata => { "categories" => "apple, strange-critter" }) end - + it "should be possible to list the categories" do @article.categories.should have(2).items @article.should be_in_category("the-apple") @@ -312,24 +312,24 @@ def create_page(options) @article.categories.first.link_text.should == "Apple" @article2.categories.first.link_text.should == "Aardvark" end - + it "should not be assigned to non-existant category" do delete_page(:category, "banana", @extension) @article.should_not be_in_category("banana") end end - + it "should set parent to nil when at root" do create_category(:path => "top-level").parent.should be_nil end - + describe "when not assigned to category" do it "should have empty category list" do article = create_article Nesta::Page.find_by_path(article.path).categories.should be_empty end end - + describe "with no content" do it "should produce no HTML output" do create_article do |path| @@ -345,7 +345,7 @@ def create_page(options) create_article @article = Nesta::Page.find_all.first end - + it "should use default layout" do @article.layout.should == :layout end @@ -353,28 +353,28 @@ def create_page(options) it "should use default template" do @article.template.should == :page end - + it "should parse heading correctly" do - @article.to_html.should have_tag("h1", "My article") + @article.to_html.should have_selector("h1", :content => "My article") end it "should use heading as link text" do @article.link_text.should == "My article" end - + it "should have default read more link text" do @article.read_more.should == "Continue reading" end - + it "should not have description" do @article.description.should be_nil end - + it "should not have keywords" do @article.keywords.should be_nil end end - + describe "with metadata" do before(:each) do @layout = 'my_layout' @@ -403,63 +403,63 @@ def create_page(options) it "should override default layout" do @article.layout.should == @layout.to_sym end - + it "should override default template" do @article.template.should == @template.to_sym end - + it "should set permalink to basename of filename" do @article.permalink.should == 'my-article' end - + it "should set path from filename" do @article.path.should == 'article-prefix/my-article' end - + it "should retrieve heading" do @article.heading.should == 'My article' end - + it "should be possible to convert an article to HTML" do - @article.to_html.should have_tag("h1", "My article") + @article.to_html.should have_selector("h1", :content => "My article") end - + it "should not include metadata in the HTML" do - @article.to_html.should_not have_tag("p", /^Date/) + @article.to_html.should_not have_selector("p:contains('Date')") end it "should not include heading in body markup" do @article.body_markup.should_not include("My article") end - + it "should not include heading in body" do - @article.body.should_not have_tag("h1", "My article") + @article.body.should_not have_selector("h1", :content => "My article") end it "should retrieve description from metadata" do @article.description.should == @description end - + it "should retrieve keywords from metadata" do @article.keywords.should == @keywords end - + it "should retrieve date published from metadata" do @article.date.strftime("%d %B %Y").should == @date end - + it "should retrieve read more link from metadata" do @article.read_more.should == @read_more end - + it "should retrieve summary text from metadata" do @article.summary.should match(/#{@summary.split('\n\n').first}/) end - + it "should treat double newline chars as paragraph break in summary" do @article.summary.should match(/#{@summary.split('\n\n').last}/) end - + it "should allow access to metadata" do @article.metadata('skillz').should == @skillz end @@ -477,13 +477,13 @@ def create_page(options) @article.link_text.should == @link_text end end - + describe "when checking last modification time" do before(:each) do create_article @article = Nesta::Page.find_all.first end - + it "should check filesystem" do mock_file_stat(:should_receive, @article.filename, "3 January 2009") @article.last_modified.should == Time.parse("3 January 2009") @@ -515,12 +515,12 @@ def create_page(options) before(:each) do stub_configuration end - + after(:each) do remove_temp_directory Nesta::FileModel.purge_cache end - + it "should still return top level menu items" do # Page.menu_items is deprecated; we're keeping it for the moment so # that we don't break themes or code in a local app.rb (just yet). @@ -532,15 +532,22 @@ def create_page(options) end describe "Markdown page" do + include ModelFactory + before(:each) do @extension = :mdown + stub_configuration + end + + after(:each) do + remove_temp_directory + Nesta::FileModel.purge_cache end it_should_behave_like "Page" it "should set heading from first h1 tag" do - page = create_page( - :heading => "First heading", :content => "# Second heading") + page = create_page(:path => "a-page", :heading => "First heading", :content => "# Second heading") page.heading.should == "First heading" end @@ -551,8 +558,19 @@ def create_page(options) end describe "Haml page" do + include ModelFactory before(:each) do @extension = :haml + stub_configuration + end + + after(:each) do + remove_temp_directory + Nesta::FileModel.purge_cache + end + + def create_page(options) + super(options.merge(:ext => @extension)) end it_should_behave_like "Page" @@ -587,8 +605,16 @@ def create_page(options) end describe "Textile page" do + include ModelFactory + before(:each) do @extension = :textile + stub_configuration + end + + after(:each) do + remove_temp_directory + Nesta::FileModel.purge_cache end it_should_behave_like "Page" diff --git a/spec/overrides_spec.rb b/spec/overrides_spec.rb index 999a9dc0..f8d82628 100644 --- a/spec/overrides_spec.rb +++ b/spec/overrides_spec.rb @@ -20,7 +20,7 @@ def create_template(type, name, content) create_fixture(type, File.join('views', 'layout.haml'), '= yield') create_fixture(type, File.join('views', name), content) end - + def create_app_file(type) create_fixture(type, 'app.rb', "DEFINED_IN_#{type.to_s.upcase}_FILE = true") end @@ -32,12 +32,12 @@ def create_app_file(type) @fixtures = [] stub_configuration end - + after(:each) do @fixtures.each { |path| FileUtils.rm(path) if File.exist?(path) } Nesta::App.root = @app_root end - + describe "when rendering stylesheets" do it "should render Sass stylesheets" do create_template(:local, 'master.sass', "body\n width: 10px * 2") @@ -61,57 +61,57 @@ def create_app_file(type) before(:each) do create_template(:local, 'page.haml', '%p Local template') end - + it "should use local application files" do create_app_file(:local) Nesta::Overrides.load_local_app Object.const_get(:DEFINED_IN_LOCAL_FILE).should be_true end - + it "should use local template in place of default" do get create_category.abspath - body.should have_tag("p", "Local template") + body.should have_selector("p:contains('Local template')") end end - + describe "when theme installed" do before(:each) do create_template(:theme, 'page.haml', '%p Theme template') end - + it "should not require theme application file automatically" do create_app_file(:theme) lambda { Object.const_get(:DEFINED_IN_THEME_FILE) }.should raise_error(NameError) end - + it "should not use theme templates automatically" do get create_category.abspath - body.should_not have_tag("p", "Theme template") + body.should_not have_selector("p:contains('Theme template')") end - + describe "and configured" do before(:each) do stub_config_key("theme", @theme) end - + it "should require theme application file" do create_app_file(:theme) Nesta::Overrides.load_theme_app Object.const_get(:DEFINED_IN_THEME_FILE).should be_true end - + it "should use theme's template in place of default" do get create_category.abspath - body.should have_tag("p", "Theme template") + body.should have_selector("p:contains('Theme template')") end - + context "and local files exist" do before(:each) do create_template(:local, "page.haml", "%p Local template") end - + it "should require local and theme application files" do create_app_file(:local) create_app_file(:theme) @@ -120,11 +120,11 @@ def create_app_file(type) Object.const_get(:DEFINED_IN_LOCAL_FILE).should be_true Object.const_get(:DEFINED_IN_THEME_FILE).should be_true end - + it "should use local template" do get create_category.abspath - body.should_not have_tag("p", "Theme template") - body.should have_tag("p", "Local template") + body.should_not have_selector("p:contains('Theme template')") + body.should have_selector("p:contains('Local template')") end end end diff --git a/spec/page_spec.rb b/spec/page_spec.rb index 34cc620e..92623a60 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -1,22 +1,22 @@ require File.expand_path('spec_helper', File.dirname(__FILE__)) require File.expand_path('model_factory', File.dirname(__FILE__)) -describe "page with keyword and description", :shared => true do +shared_examples_for "page with keyword and description" do it "should set the keywords meta tag" do do_get - body.should have_tag("meta[@name=keywords][@content='#{@keywords}']") + body.should have_xpath("//meta[@name='keywords'][@content='#{@keywords}']") end it "should set description meta tag" do do_get - body.should have_tag("meta[@name=description][@content='#{@description}']") + body.should have_xpath("//meta[@name='description'][@content='#{@description}']") end end -describe "page that can display menus", :shared => true do +shared_examples_for "page that can display menus" do it "should not display menu by default" do do_get - body.should_not have_tag("#sidebar ul.menu") + body.should_not have_selector("#sidebar ul.menu") end describe "and simple menu configured" do @@ -26,11 +26,10 @@ it "should link to top level menu items" do do_get - body.should have_tag( - "ul.menu a[@href$=#{@category.abspath}]", /#{@category.link_text}/) + body.should have_selector "ul.menu a[@href$='#{@category.abspath}']:contains('#{@category.link_text}')" end end - + describe "and nested menu configured" do before(:each) do @level2 = create_category(:path => "level-2", :heading => "Level 2", @@ -46,12 +45,12 @@ it "should display first level of nested sub menus" do do_get - body.should have_tag("ul.menu li ul li a", Regexp.new(@level2.link_text)) + body.should have_selector("ul.menu li ul li a:contains('#{@level2.link_text}')") end it "should not display nested menus to arbitrary depth" do do_get - body.should_not have_tag("ul.menu li ul li ul") + body.should_not have_selector("ul.menu li ul li ul") end end @@ -64,7 +63,7 @@ create_menu(text) template_path = File.expand_path( 'templates', File.dirname(File.dirname(__FILE__))) - @default_homepage_content = File.read(File.join(template_path, + @default_homepage_content = File.read(File.join(template_path, 'index.haml')) end @@ -74,9 +73,9 @@ :ext => :haml, :content => @default_homepage_content) do_get - body.should have_tag("ul.menu a[@href='/']", /Home/) + body.should have_selector("ul.menu a[@href='/']:contains('Home')") end - + it "should use the heading if it exists" do create_page( :path => 'index', @@ -84,7 +83,7 @@ :heading => 'My heading', :content => @default_homepage_content) do_get - body.should have_tag("ul.menu a[@href='/'", /My heading/) + body.should have_selector("ul.menu a[@href='/']:contains('My heading')") end it "should use the link text if specified" do @@ -95,7 +94,7 @@ :content => @default_homepage_content, :metadata => {'link text'=>'My link text'}) do_get - body.should have_tag("ul.menu a[@href='/'", /My link text/) + body.should have_selector("ul.menu a[@href='/']:contains('My link text')") end end end @@ -103,25 +102,25 @@ describe "The layout" do include ModelFactory include RequestSpecHelper - + it "should not include GA JavaScript by default" do stub_configuration get "/" - body.should_not have_tag("script", /'_setAccount', 'UA-1234'/) + body.should_not have_selector("script", :content => "'_setAccount', 'UA-1234'") end - + it "should include GA JavaScript if configured" do stub_config_key('google_analytics_code', 'UA-1234', :rack_env => true) stub_configuration get '/' - body.should have_tag('script', /'_setAccount', 'UA-1234'/) + body.should have_selector('script', :content => "'_setAccount', 'UA-1234'") end end describe "The home page" do include ModelFactory include RequestSpecHelper - + before(:each) do stub_configuration template_path = File.expand_path( @@ -134,7 +133,7 @@ ) create_category end - + after(:each) do remove_temp_directory Nesta::FileModel.purge_cache @@ -143,43 +142,42 @@ def do_get get "/" end - + describe "when categories exist" do before(:each) do @category = create_category end - + it_should_behave_like "page that can display menus" end - + it "should render successfully" do do_get last_response.should be_ok end - + it "should display site title in hgroup tag" do - pending "Hpricot doesn't support HTML5" - body.should have_tag('hgroup h1', /My blog/) + do_get + body.should have_selector('hgroup h1', :content => "My blog") end - + it "should display site subtitle in hgroup tag" do - pending "Hpricot doesn't support HTML5" do_get - body.should have_tag('hgroup h2', /about stuff/) + body.should have_selector('hgroup h2', :content => "about stuff") end - + describe "when articles have no summary" do before(:each) do create_article do_get end - + it "should display full content of article" do - body.should have_tag("p", "Content goes here") + body.should have_selector("p", :content => "Content goes here") end - + it "should not display read more link" do - body.should_not have_tag("a", /continue/i) + body.should_not have_selector("a", :content => 'continue') end end @@ -193,18 +191,18 @@ def do_get }) do_get end - + it "should display link to article in h2 tag" do - body.should have_tag( - "h1 a[@href$=#{@article.abspath}]", /^\s*#{@article.link_text}$/) + body.should have_selector( + "h1 a[@href$='#{@article.abspath}']:contains('#{@article.link_text}')") end - + it "should display article summary if available" do - body.should have_tag('p', @summary.split('\n\n').first) + body.should have_selector('p', :content => @summary.split('\n\n').first) end - + it "should display read more link" do - body.should have_tag("a[@href$=#{@article.abspath}]", @read_more) + body.should have_selector("a[@href$='#{@article.abspath}']", :content => @read_more) end end end @@ -212,7 +210,7 @@ def do_get describe "An article" do include ModelFactory include RequestSpecHelper - + before(:each) do stub_configuration @date = '07 September 2009' @@ -228,12 +226,12 @@ def do_get 'link text' => @link_text }) end - + after(:each) do remove_temp_directory Nesta::FileModel.purge_cache end - + def do_get get @article.abspath end @@ -244,7 +242,7 @@ def do_get before(:each) do @category = create_category end - + it_should_behave_like "page that can display menus" end @@ -255,24 +253,24 @@ def do_get it "should display the heading" do do_get - body.should have_tag('h1', 'My article') + body.should have_selector('h1', :content => 'My article') end it "should use link text for title tag" do do_get - body.should have_tag('title', "#{@link_text}") + body.should have_selector('title', :content => @link_text) end it "should display the date" do do_get - body.should have_tag('time', @date) + body.should have_selector('time', :content => @date) end it "should display the content" do do_get - body.should have_tag('p', 'Content goes here') + body.should have_selector('p', :content => 'Content goes here') end - + describe "that is assigned to categories" do before(:each) do create_category(:heading => 'Apple', :path => 'the-apple') @@ -282,26 +280,24 @@ def do_get :metadata => { 'categories' => 'banana, the-apple' } ) end - + it "should render successfully" do do_get last_response.should be_ok end - + it "should link to each category" do - pending "Hpricot doesn't support HTML5" do_get - body.should have_tag("nav.categories") do |categories| - categories.should have_tag("a[@href=/banana]", "Banana") - categories.should have_tag("a[@href=/the-apple]", "Apple") + body.should have_selector("p.meta") do |categories| + categories.should have_selector("a[href='/banana']", :content => "Banana") + categories.should have_selector("a[href='/the-apple']", :content => "Apple") end end it "should link to a category in breadcrumb" do - pending "Hpricot doesn't support HTML5" do_get - body.should have_tag( - "nav.breadcrumb/a[@href=#{@category.abspath}]", @category.link_text) + body.should have_selector( + "nav.breadcrumb a[href='#{@category.abspath}']", :content => @category.link_text) end end end @@ -309,7 +305,7 @@ def do_get describe "A page" do include ModelFactory include RequestSpecHelper - + before(:each) do stub_configuration end @@ -318,7 +314,7 @@ def do_get remove_temp_directory Nesta::FileModel.purge_cache end - + def do_get get @category.abspath end @@ -365,17 +361,17 @@ def do_get it "should display the heading" do do_get - body.should have_tag('h1', @category.heading) + body.should have_selector('h1', :content => @category.heading) end it "should use title metadata to set heading" do do_get - body.should have_tag('title', @title) + body.should have_selector('title', :content => @title) end it "should display the content" do do_get - body.should have_tag("p", @content) + body.should have_selector("p", :content => @content) end describe "with associated pages" do @@ -398,8 +394,8 @@ def do_get it "should list highest priority pages at the top" do do_get - body.should have_tag('li:nth-child(1) h1 a', 'Category 2') - body.should have_tag('li:nth-child(2) h1 a', 'Category 1') + body.should have_selector('li:nth-child(1) h1 a', :content => 'Category 2') + body.should have_selector('li:nth-child(2) h1 a', :content => 'Category 1') end end @@ -418,33 +414,33 @@ def do_get it "should display links to articles" do do_get - body.should have_tag( - "h1 a[@href$='#{@article.abspath}']", /^\s*#{@article.link_text}$/) - body.should_not have_tag("h3", @article2.link_text) + body.should have_selector( + "h1 a[@href$='#{@article.abspath}']", :content => @article.link_text) + body.should_not have_selector("h3", :content => @article2.link_text) end it "should display the article heading" do do_get - body.should have_tag('h1', @articles_heading) + body.should have_selector('h1', :content => @articles_heading) end end it "should not include Disqus comments by default" do do_get - body.should_not have_tag('#disqus_thread') + body.should_not have_selector('#disqus_thread') end end - + describe "that is configured to show Disqus comments" do before(:each) do stub_config_key("disqus_short_name", "mysite") @category = create_category end - + it "should display Disqus comments" do do_get - body.should have_tag('#disqus_thread') - body.should have_tag('script[@src*="mysite.disqus.com/embed.js"]') + body.should have_selector('#disqus_thread') + body.should have_selector('script[@src*="mysite.disqus.com/embed.js"]') end end end @@ -470,7 +466,7 @@ def do_get :heading => "A Page" ) get "/a-page" - body.should have_tag("div", "23 November 2010") + body.should have_selector("div", :content => "23 November 2010") end it "should access helpers when rendering articles on a category page" do @@ -487,7 +483,7 @@ def do_get :content => "%h1 Second heading\n\n%div= format_date(Date.new(2010, 11, 23))" ) get "/a-page" - body.should have_tag("div", "23 November 2010") + body.should have_selector("div", :content => "23 November 2010") end end @@ -504,23 +500,23 @@ def do_get remove_temp_directory Nesta::FileModel.purge_cache end - + describe "in the attachments folder" do before(:each) do path = File.join(Nesta::Config.attachment_path, 'test.txt') File.open(path, 'w') { |file| file.write("I'm a test attachment") } end - + it "should be served successfully" do get "/attachments/test.txt" last_response.should be_ok end - + it "should be sent to the client" do get "/attachments/test.txt" body.should include("I'm a test attachment") end - + it "should set the appropriate MIME type" do get "/attachments/test.txt" last_response.headers["Content-Type"].should =~ Regexp.new("^text/plain") diff --git a/spec/sitemap_spec.rb b/spec/sitemap_spec.rb index cf64ae51..3757c93b 100644 --- a/spec/sitemap_spec.rb +++ b/spec/sitemap_spec.rb @@ -4,7 +4,7 @@ describe "sitemap XML" do include RequestSpecHelper include ModelFactory - + before(:each) do stub_configuration @category = create_category do |path| @@ -15,75 +15,75 @@ end get "/sitemap.xml" end - + after(:each) do Nesta::FileModel.purge_cache remove_temp_directory end - + it "should render successfully" do last_response.should be_ok end - + it "should have a urlset tag" do namespace = "http://www.sitemaps.org/schemas/sitemap/0.9" - body.should have_tag("/urlset[@xmlns=#{namespace}]") + body.should have_xpath("//urlset[@xmlns='#{namespace}']") end - + it "should reference the home page" do - body.should have_tag("/urlset/url/loc", "http://example.org/") + body.should have_xpath("//urlset/url/loc", :content => "http://example.org/") end - + it "should configure home page to be checked frequently" do - body.should have_tag("/urlset/url") do |url| - url.should have_tag("loc", "http://example.org/") - url.should have_tag("changefreq", "daily") - url.should have_tag("priority", "1.0") + body.should have_xpath("//urlset/url") do |url| + url.should have_xpath("loc", :content => "http://example.org/") + url.should have_xpath("changefreq", :content => "daily") + url.should have_xpath("priority", :content => "1.0") end end - + it "should set the homepage lastmod from latest article" do - body.should have_tag("/urlset/url") do |url| - url.should have_tag("loc", "http://example.org/") - url.should have_tag("lastmod", /^2009-01-03T15:10:00/) + body.should have_xpath("//urlset/url") do |url| + url.should have_xpath("loc", :content => "http://example.org/") + url.should have_selector("lastmod:contains('2009-01-03T15:10:00')") end end - + it "should reference category pages" do - body.should have_tag( - "/urlset/url/loc", "http://example.org/#{@category.path}") + body.should have_xpath( + "//urlset/url/loc", :content => "http://example.org/#{@category.path}") end - + it "should reference article pages" do - body.should have_tag( - "/urlset/url/loc", "http://example.org/#{@article.path}") + body.should have_xpath( + "//urlset/url/loc", :content => "http://example.org/#{@article.path}") end end describe "sitemap XML lastmod" do include ModelFactory include RequestSpecHelper - + before(:each) do stub_configuration end - + after(:each) do remove_temp_directory Nesta::FileModel.purge_cache end - + it "should be set for file based page" do create_article do |path| mock_file_stat(:stub!, path, "3 January 2009, 15:37:01") end get "/sitemap.xml" - body.should have_tag("url") do |url| - url.should have_tag("loc", /my-article$/) - url.should have_tag("lastmod", /^2009-01-03T15:37:01/) + body.should have_selector("url") do |url| + url.should have_selector("loc:contains('my-article')") + url.should have_selector("lastmod:contains('2009-01-03T15:37:01')") end end - + it "should be set to latest page for home page" do create_article(:path => "article-1") do |path| mock_file_stat(:stub!, path, "4 January 2009") @@ -92,9 +92,9 @@ mock_file_stat(:stub!, path, "3 January 2009") end get "/sitemap.xml" - body.should have_tag("url") do |url| - url.should have_tag("loc", "http://example.org/") - url.should have_tag("lastmod", /^2009-01-04/) + body.should have_selector("url") do |url| + url.should have_selector("loc:contains('http://example.org/')") + url.should have_selector("lastmod:contains('2009-01-04')") end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e237528f..5a41d9b4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,11 +1,9 @@ -require 'rubygems' -require 'spec' -require 'spec/interop/test' +require 'rspec' require 'rack/test' -require 'rspec_hpricot_matchers' require 'sinatra' - -Test::Unit::TestCase.send :include, Rack::Test::Methods +require 'test/unit' +require 'webrat' +require 'webrat/core/matchers' module Nesta class App < Sinatra::Base @@ -33,7 +31,7 @@ def stub_config_key(key, value, options = {}) @config[key] = value end end - + def stub_configuration(options = {}) stub_config_key('title', 'My blog', options) stub_config_key('subtitle', 'about stuff', options) @@ -52,24 +50,24 @@ def create_temp_directory def remove_temp_directory FileUtils.rm_r(TempFileHelper::TEMP_DIR, :force => true) end - + def temp_path(base) File.join(TempFileHelper::TEMP_DIR, base) end end -Spec::Runner.configure do |config| - config.include(RspecHpricotMatchers) - +RSpec.configure do |config| config.include(ConfigSpecHelper) config.include(TempFileHelper) + config.include(Rack::Test::Methods) + include Webrat::Matchers end module RequestSpecHelper def app Nesta::App end - + def body last_response.body end