Skip to content
Browse files

Tests for three out of four article controller actions.

  • Loading branch information...
1 parent 5f79834 commit c059cb9cba53430221772ef0918f9635000610f0 @mike-burns committed Mar 17, 2012
View
2 app/controllers/articles_controller.rb
@@ -9,7 +9,7 @@ def new
def create
eventually(CreateAnArticle, params[:article])
- redirect_to articles_path
+ redirect_to articles_url
end
def show
View
40 spec/controllers/articles_controller_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe ArticlesController do
+ it 'assigns one article' do
+ article = create(:article)
+ get :show, :id => article.id
+ assigns(:article).should == article
+ end
+
+ it 'eventually creates an article' do
+ fake_enqueuer = FakeEnqueuer.new
+ AppropriateQueue.enqueuer = fake_enqueuer
+ article_params = { :title => 'the title', :body => 'the body' }
+
+ post :create, :article => article_params
+
+ response.should redirect_to(articles_url)
+ fake_enqueuer.should have_enqueued(CreateAnArticle).
+ with(article_params)
+ end
+
+ it 'assigns the new article form for a blank article' do
+ get :new
+ assigns(:article).attributes.should == Article.new.attributes
+ end
+
+ it 'assigns the new article form given a JSON-ified article' do
+ expected_article = Article.new(:title => 'hello').tap do |article|
+ article.errors.add(:body, "can't be blank")
+ end
+ json = ActiveRecordMarshaller.new(expected_article).to_json
+
+ get :new, :json => json
+
+ assigns(:article).attributes.should == expected_article.attributes
+ assigns(:article).errors.to_hash.should == expected_article.errors.to_hash
+ end
+
+ it 'assigns the list of all articles, including pending'
+end
View
31 spec/support/fake_enqueuer.rb
@@ -0,0 +1,31 @@
+class FakeEnqueuer
+ def initialize
+ @queue = []
+ end
+
+ def enqueue(constant, announcer, data)
+ stringified_data = data.inject({}) do |hash,(key,value)|
+ hash.merge(key.to_s => value)
+ end
+
+ @queue << [constant, stringified_data]
+ end
+
+ def queue_contains?(element)
+ @queue.include?(element)
+ end
+end
+
+RSpec::Matchers.define :have_enqueued do |constant|
+ chain :with do |data|
+ stringified_data = data.inject({}) do |hash,(key,value)|
+ hash.merge(key.to_s => value)
+ end
+
+ @data = stringified_data
+ end
+
+ match do |fake_queue|
+ fake_queue.queue_contains?([constant, @data])
+ end
+end

0 comments on commit c059cb9

Please sign in to comment.
Something went wrong with that request. Please try again.