Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Spec coverage for Rails 4 strong params functionality.

Added factory_girl factories for use with update specs on:

admin/comments_controller_spec.rb
admin/posts_controller_spec.rb
  • Loading branch information...
commit 48185aa5d9c440189e43578f1599b971c5188e4a 1 parent d930136
@gaelian gaelian authored
View
23 spec/controllers/admin/comments_controller_spec.rb
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/../../spec_helper'
+require File.dirname(__FILE__) + '/../../factories'
require 'json'
describe Admin::CommentsController do
@@ -84,6 +85,28 @@ def do_put
end
end
+ describe 'handling PUT to update with expected whitelisted attributes present' do
+ before(:each) do
+ @comment = FactoryGirl.create(:comment)
+ Comment.stub(:find).and_return(@comment)
+ end
+
+ it 'allows whitelisted attributes as expected' do
+ session[:logged_in] = true
+ put :update, :id => 1, :comment => {
+ 'author' => "Don Alias",
+ 'author_url' => "http://example.com",
+ 'author_email' => "donalias@example.com",
+ 'body' => "This is a comment"
+ }
+
+ assigns(:comment).author.should == "Don Alias"
+ assigns(:comment).author_url.should == "http://example.com"
+ assigns(:comment).author_email.should == "donalias@example.com"
+ assigns(:comment).body.should == "This is a comment"
+ end
+ end
+
describe 'handling DELETE to destroy' do
before(:each) do
@comment = Comment.new
View
48 spec/controllers/admin/posts_controller_spec.rb
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/../../spec_helper'
+require File.dirname(__FILE__) + '/../../factories'
describe Admin::PostsController do
describe 'handling GET to index' do
@@ -105,6 +106,32 @@ def do_put
end
end
+ describe 'handling PUT to update with expected whitelisted attributes present' do
+ before(:each) do
+ @post = FactoryGirl.create(:post)
+ Post.stub(:find).and_return(@post)
+ end
+
+ it 'allows whitelisted attributes as expected' do
+ session[:logged_in] = true
+ put :update, :id => 1, :post => {
+ 'title' => "My Updated Post",
+ 'body' => "hello this is my updated post",
+ 'tag_list' => "red, green, blue, magenta",
+ 'published_at_natural' => "1 hour from now",
+ 'slug' => "my-manually-entered-updated-post-slug",
+ 'minor_edit' => "1"
+ }
+
+ assigns(:post).title.should == "My Updated Post"
+ assigns(:post).body.should == "hello this is my updated post"
+ assigns(:post).tag_list.should == ["red", "green", "blue", "magenta"]
+ assigns(:post).published_at_natural.should == "1 hour from now"
+ assigns(:post).slug.should == "my-manually-entered-updated-post-slug"
+ assigns(:post).minor_edit.should == "1"
+ end
+ end
+
describe 'handling POST to create with valid attributes' do
it 'creates a post' do
session[:logged_in] = true
@@ -112,6 +139,27 @@ def do_put
end
end
+ describe 'handling POST to create with expected whitelisted attributes present' do
+ it 'allows whitelisted attributes as expected' do
+ session[:logged_in] = true
+ put :create, :id => 1, :post => {
+ 'title' => "My Awesome New Post",
+ 'body' => "hello this is my awesome new post",
+ 'tag_list' => "teal, azure, turquoise",
+ 'published_at_natural' => "now",
+ 'slug' => "my-manually-entered-slug",
+ 'minor_edit' => "0"
+ }
+
+ assigns(:post).title.should == "My Awesome New Post"
+ assigns(:post).body.should == "hello this is my awesome new post"
+ assigns(:post).tag_list.should == ["teal", "azure", "turquoise"]
+ assigns(:post).published_at_natural.should == "now"
+ assigns(:post).slug.should == "my-manually-entered-slug"
+ assigns(:post).minor_edit.should == "0"
+ end
+ end
+
def valid_post_attributes
{
'title' => "My Post",
View
2  spec/controllers/comments_controller_spec.rb
@@ -73,7 +73,7 @@ def mock_post!
:author_url => 'http://www.enkiblog.com',
:author_email => 'donalias@enkiblog.com',
:created_at => @created_at = 1.year.ago,
- :updated_at => @updated_at = 1.year.ago,
+ :updated_at => @updated_at = 1.year.ago
}
end
View
22 spec/factories.rb
@@ -0,0 +1,22 @@
+require 'factory_girl'
+
+# These factories are used by some rspec specs, as opposed
+# to the ones used by cucumber which are defined elsewhere.
+FactoryGirl.define do
+ factory :post do
+ title 'My Post'
+ body 'hello this is my post'
+ tag_list 'red, green, blue'
+ published_at_natural 'now'
+ slug 'my-manually-entered-slug'
+ minor_edit '0'
+ end
+
+ factory :comment do
+ author 'Don Pseudonym'
+ author_email 'donpseudonym@enkiblog.com'
+ author_url 'http://enkiblog.com'
+ body 'Not all those who wander are lost'
+ association :post
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.