From 8c2e844ac3d839b222ae54a351f28215198602a9 Mon Sep 17 00:00:00 2001 From: "p.bjorklund@gmail.com" Date: Mon, 11 Feb 2013 14:43:59 +0100 Subject: [PATCH 1/3] Add another_blog_post fabricator --- spec/fabricators/blog_post_fabricator.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/fabricators/blog_post_fabricator.rb b/spec/fabricators/blog_post_fabricator.rb index 561914bb..ff271066 100644 --- a/spec/fabricators/blog_post_fabricator.rb +++ b/spec/fabricators/blog_post_fabricator.rb @@ -2,3 +2,8 @@ content "Hello, world" title "Title" end + +Fabricator(:another_blog_post, from: :blog_post) do + content "World, Hello" + title "Eltit" +end From 6bf8669045b7f47b59df33e65a27fed63942c39a Mon Sep 17 00:00:00 2001 From: "p.bjorklund@gmail.com" Date: Mon, 11 Feb 2013 14:45:41 +0100 Subject: [PATCH 2/3] Use blog_index_path since blog_index does not exist --- app/controllers/blog_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/blog_controller.rb b/app/controllers/blog_controller.rb index ae9542b4..7f06d3be 100644 --- a/app/controllers/blog_controller.rb +++ b/app/controllers/blog_controller.rb @@ -10,13 +10,13 @@ def show end def admin - redirect_to blog_index unless current_user.blog_poster + redirect_to blog_index_path unless current_user.blog_poster @post = BlogPost.new @posts = BlogPost.all.reverse end def create - redirect_to blog_index unless current_user.blog_poster + redirect_to blog_index_path and return unless current_user.blog_poster @post = BlogPost.create(params[:blog_post]) redirect_to admin_blog_index_path, :notice => "Blog Post created!" end From ffe7a76c09e0150f79ebc4749f08e93d0d12ce11 Mon Sep 17 00:00:00 2001 From: "p.bjorklund@gmail.com" Date: Mon, 11 Feb 2013 14:45:51 +0100 Subject: [PATCH 3/3] Specs for BlogController --- spec/controllers/blog_controller_spec.rb | 99 ++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/spec/controllers/blog_controller_spec.rb b/spec/controllers/blog_controller_spec.rb index 2dda864e..8da8d9df 100644 --- a/spec/controllers/blog_controller_spec.rb +++ b/spec/controllers/blog_controller_spec.rb @@ -1,5 +1,104 @@ require 'spec_helper' describe BlogController do + let(:blog_post) { Fabricate(:blog_post) } + let(:another_blog_post) { Fabricate(:another_blog_post) } + describe('#index') do + it "gets index successfully" do + get :index + response.should be_successful + end + + describe("handles posts by") do + before(:each) do + blog_post + another_blog_post + get :index + @posts = assigns(:posts) + end + it "fetching posts" do + @posts.size.should == 2 + end + it "sorting them in reverse order" do + @posts.first.content[0].should > @posts.last.content[0] + end + end + end + + describe "#show" do + before(:each) do + blog_post + get :show, id: blog_post + end + it { response.should be_successful } + it { assigns(:post).should == blog_post } + end + + describe "#admin" do + context "when user is not signed in" do + before(:each) do + get :create + end + #TODO Should it not use signin_path? + it { response.should redirect_to(new_user_session_path) } + end + + context "when user is not an blog_poster" do + before(:each) do + blog_post + get :admin + end + it { response.should be_redirect } + it { assigns(:post).should be_nil } + it { assigns(:posts).should be_nil } + end + context "when user is a blog_poster" do + before(:each) do + sign_in Fabricate(:user, blog_poster: true) + blog_post + get :admin + end + it { response.should be_successful } + it { assigns(:post).should be_a_new(BlogPost) } + it { assigns(:posts).size.should == 1 } + end + end + + describe "#create" do + context "when user is not signed in" do + before(:each) do + get :create + end + #TODO Should it not use signin_path? + it { response.should redirect_to(new_user_session_path) } + end + + context "when user is not an blog_poster" do + before(:each) do + sign_in Fabricate(:user) + get :create + end + + it { response.should redirect_to(blog_index_path) } + + it { assigns(:post).should be_nil } + end + + context "when user is a blog_poster" do + before(:each) do + sign_in Fabricate(:user, blog_poster: true) + end + + it "redirects to admin_blog_index_path when user is blog_poster" do + post :create + response.should redirect_to(admin_blog_index_path) + end + + it "creates a new blogpost" do + ->{ post :create, blog_post: Fabricate.build(:blog_post).attributes } + .should change(BlogPost, :count).by(1) + end + end + end end