Skip to content

Instantly share code, notes, and snippets.

@r38y
Created September 21, 2008 18:59
Show Gist options
  • Save r38y/11891 to your computer and use it in GitHub Desktop.
Save r38y/11891 to your computer and use it in GitHub Desktop.
class MyAccount::PostsController < ApplicationController
before_filter :login_required
def index
@posts = current_user.posts.paginate :page => params[:page], :order => 'created_at DESC'
end
def toggle_visibility
@post = current_user.posts.find params[:id]
@post.toggle!(:visible)
respond_to do |format|
format.html { redirect_to my_account_posts_url(:page => params[:page]) }
format.js {}
end
end
end
require File.dirname(__FILE__) + '/../../spec_helper'
describe MyAccount::PostsController do
describe "handling GET /my_account/posts" do
before do
stub_account
stub_current_user
@post = mock_model(Post)
@posts = stub("posts", :paginate => [@post])
@user.stub!(:posts).and_return @posts
end
def do_get
get :index
end
it "should be successful for an authenticated user" do
do_get
response.should be_success
end
it "should redirect to login page for a non-authenticated user" do
controller.stub!(:current_user).and_return nil
do_get
response.should redirect_to(login_url)
end
it "should paginate posts" do
@user.posts.should_receive(:paginate).and_return [@post]
do_get
end
it "should find posts" do
@user.should_receive(:posts).and_return @posts
do_get
end
it "should redirect to the about page if an account can't be found" do
User.stub!(:find_by_subdomain).and_return nil
do_get
response.should redirect_to(about_path)
end
end
describe "handling GET /my_account/posts/:id/toggle_visibility" do
before do
stub_account
stub_current_user
@post = mock_model(Post)
@posts = stub("posts", :find => @post)
@user.stub!(:posts).and_return @posts
@post.stub!(:toggle!)
end
def do_get
get :toggle_visibility, :id => @post.id
end
it "should redirect to login page for a non-authenticated user" do
controller.stub!(:current_user).and_return nil
do_get
response.should redirect_to(login_url)
end
it "should redirect to the about page if an account can't be found" do
User.stub!(:find_by_subdomain).and_return nil
do_get
response.should redirect_to(about_path)
end
# fails when all tests are run
# ArgumentError in 'MyAccount::PostsController handling GET /my_account/posts/:id/toggle_visibility should find a post'
# wrong number of arguments (0 for 1)
# /umlatte/projects/isfeasting/app/controllers/my_account/posts_controller.rb:13:in `js'
# /umlatte/projects/isfeasting/app/controllers/my_account/posts_controller.rb:13:in `toggle_visibility'
it "should find a post" do
@user.posts.should_receive(:find).and_return(@post)
do_get
end
# fails when all tests are run
it "should toggle the posts visibility" do
@post.should_receive(:toggle!)
do_get
end
describe "with javascript" do
def do_get
get :toggle_visibility, :id => @post.id, :format => 'js'
end
# fails when all tests are run
it "should be successful" do
do_get
response.should be_success
end
# fails when all tests are run
it "should render the toggle_visibility.js.erb template" do
do_get
response.should render_template('toggle_visibility')
end
end
describe "without javascript" do
# fails when all tests are run
it "should redirect to the list of posts" do
do_get
response.should redirect_to(my_account_posts_path)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment