Permalink
Browse files

New RSpec-based specs, with factory_girl

  • Loading branch information...
1 parent bd2b453 commit 32ca93a13816297f18e0760bb22cd41749989ff5 @hmans committed Feb 17, 2012
View
1 .rspec
@@ -0,0 +1 @@
+--color
View
11 .watchr
@@ -1,14 +1,15 @@
-def run(cmd)
+def run(cmd, msg = nil)
+ puts "=== %s" % msg if msg
puts "=== %s" % cmd
system cmd
puts "\n"
end
-watch("test/.*_test\.rb") { |m| run("bundle exec ruby %s" % m[0]) }
-watch("lib/schreihals/(.*)\.rb") { |m| run("bundle exec ruby test/%s_test.rb" % m[1]) }
-watch('^test/test_helper\.rb') { run "rake test" }
+watch("spec/.*_spec\.rb") { |m| run("bundle exec rspec %s" % m[0]) }
+watch("lib/schreihals/(.*)\.rb") { |m| run("bundle exec rspec spec/%s_spec.rb" % m[1]) }
+watch('^spec/(spec_helper|factories)\.rb') { |f| run "bundle exec rake spec", "%s.rb has been modified" % f }
# Ctrl-\
-Signal.trap('QUIT') { run("rake test") }
+Signal.trap('QUIT') { run("bundle exec rake spec") }
# Ctrl-C
Signal.trap('INT') { abort("\nQuitting.") }
View
@@ -1,12 +1,7 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
-# integrate riot
-require 'rake/testtask'
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
- test.pattern = 'test/*_test.rb'
- test.verbose = true
-end
-
-task :default => :test
+# integrate rspec
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new('spec')
+task :default => :spec
View
@@ -17,5 +17,13 @@
Sass::Engine::DEFAULT_OPTIONS[:load_paths].unshift(File.expand_path("../views", __FILE__))
Sass::Engine::DEFAULT_OPTIONS[:load_paths].unshift(File.expand_path("./views"))
+Mongoid.logger.level = 3
+
module Schreihals
+ mattr_reader :mongo_uri
+
+ def self.mongo_uri=(uri)
+ Mongoid::Config.from_hash("uri" => uri)
+ @@mongo_uri = uri
+ end
end
View
@@ -20,11 +20,6 @@ class App < Sinatra::Base
include Schreihals::Actions::Admin
include Schreihals::Actions::Blog
- def initialize(*args)
- super
- Mongoid::Config.from_hash("uri" => settings.mongo_uri)
- end
-
configure do
set :blog_title, "My Schreihals Blog"
set :blog_description, ""
@@ -36,7 +31,6 @@ def initialize(*args)
set :twitter_id, nil
set :footer, ""
set :administrator, nil
- set :mongo_uri, ENV['MONGOLAB_URI'] || ENV['MONGOHQ_URL'] || ENV['MONGO_URL'] || 'mongodb://localhost/schreihals'
end
def cache_for(time)
View
@@ -13,7 +13,7 @@
namespace :db do
desc 'Import Heroku database to local database'
task :pull do
- system 'MONGO_URL="mongodb://localhost/schreihals" heroku mongo:pull'
+ system "MONGO_URL=\"#{Schreihals.mongo_uri}\" heroku mongo:pull"
end
task :push do
View
@@ -44,8 +44,10 @@ Gem::Specification.new do |gem|
gem.add_dependency 'wirble'
# development dependencies
+ gem.add_development_dependency 'rspec', '>= 2.8.0'
+ gem.add_development_dependency 'database_cleaner'
+ gem.add_development_dependency 'factory_girl'
gem.add_development_dependency 'shotgun'
- gem.add_development_dependency 'riot'
gem.add_development_dependency 'rack-test'
gem.add_development_dependency 'watchr'
gem.add_development_dependency 'awesome_print'
View
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe Schreihals::App do
+ include Rack::Test::Methods
+
+ def app
+ Schreihals::App
+ end
+
+ it "says hello" do
+ get '/'
+ last_response.should be_ok
+ end
+end
View
@@ -0,0 +1,5 @@
+FactoryGirl.define do
+ factory :post, class: Schreihals::Post do
+ title "It's a Post!"
+ end
+end
View
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe Schreihals::Post do
+ subject do
+ Factory.build(:post, published_at: "2012-01-02 12:23:13")
+ end
+
+ context "date methods" do
+ its(:year) { should == 2012 }
+ its(:month) { should == 01 }
+ its(:day) { should == 02 }
+ end
+end
View
@@ -0,0 +1,32 @@
+SPEC_DIR = File.dirname(__FILE__)
+lib_path = File.expand_path("#{SPEC_DIR}/../lib")
+$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
+
+require 'rubygems'
+require 'bundler/setup'
+
+require 'schreihals'
+
+Schreihals.mongo_uri = 'mongodb://localhost/schreihals_test'
+
+require 'awesome_print'
+require 'rack/test'
+require 'database_cleaner'
+require 'factory_girl'
+require File.expand_path("../factories.rb", __FILE__)
+
+set :environment, :test
+
+RSpec.configure do |config|
+ config.before(:suite) do
+ DatabaseCleaner[:mongoid].strategy = :truncation
+ end
+
+ config.before(:each) do
+ DatabaseCleaner[:mongoid].start
+ end
+
+ config.after(:each) do
+ DatabaseCleaner[:mongoid].clean
+ end
+end
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,62 @@
+require_relative 'test_helper'
+
+context Schreihals::Post do
+ # setup { Rack::MockRequest.new(MyTestApp.new) }
+
+ # context "when loading the home page" do
+ # setup { topic.get '/' }
+
+ # asserts(:status).equals 200
+ # asserts(:body).present
+ # asserts(:content_type).equals 'text/html;charset=utf-8'
+ # asserts(:body).includes_elements("section.posts article.post", 2)
+
+ # asserts(:body).includes_html('title' => MyTestApp.settings.blog_title)
+ # asserts(:body).includes_html('.container>footer' => MyTestApp.settings.footer)
+ # end
+
+ # context "when loading a specific post's page" do
+ # setup { topic.get '/2011/12/23/first-post/' }
+
+ # asserts(:status).equals 200
+ # asserts(:body).present
+ # asserts(:content_type).equals 'text/html;charset=utf-8'
+ # asserts(:body).includes_elements("article.post", 1)
+ # asserts(:body).includes_html('article.post header h2 a' => "First Post\.")
+ # end
+
+ # context "when loading a static page" do
+ # setup { topic.get '/static-page/' }
+
+ # asserts(:status).equals 200
+ # asserts(:body).present
+ # asserts(:content_type).equals 'text/html;charset=utf-8'
+ # asserts(:body).includes_elements("article.post", 1)
+ # asserts(:body).includes_html('article.post header h2 a' => "A Static Page\.")
+ # end
+
+ # context "when loading the ATOM feed" do
+ # setup { topic.get '/atom.xml' }
+
+ # asserts(:status).equals 200
+ # asserts(:body).present
+ # asserts(:content_type).equals 'application/xml+atom'
+ # asserts(:body).includes_elements("entry", 2)
+ # end
+
+ # context "when loading a static asset provided by schreihals" do
+ # setup { topic.get '/favicon.ico' }
+
+ # asserts(:status).equals 200
+ # asserts(:body).present
+ # asserts(:content_type).equals 'image/vnd.microsoft.icon'
+ # end
+
+ # context "when loading an invalid URL" do
+ # setup { topic.get '/ooga/booga/foo/to/the/bar' }
+
+ # asserts(:status).equals 404
+ # asserts(:body).present
+ # asserts(:content_type).equals 'text/html;charset=utf-8'
+ # end
+end
File renamed without changes.
File renamed without changes.

0 comments on commit 32ca93a

Please sign in to comment.