Report abuse

Using RSpec

require File.dirname(__FILE__) + '/../spec_helper'

module PhotoSetsControllerSpecHelper
  def login(user = nil)
    @viewer = user || mock_model(User)
    controller.stub!(:current_user).and_return(@viewer)
  end

  def stub_owner(attributes = {})
    default_attributes = {:profile => mock_model(Profile)}
    @owner = mock_model(User, default_attributes.merge(attributes))
    User.stub!(:find).and_return(@owner)
  end

  def stub_tabs
    controller.stub!(:load_profile_tabs)
  end

  def valid_attributes
    {'title' => 'Naked Shots'}
  end

end

describe "user tab page", :shared => true do

  it "should load profile tabs" do
    controller.should_receive(:load_profile_tabs)
    make_request
  end

end

describe PhotoSetsController, "when logged out" do

  it "create should redirect to login" do
    post :create, :user_id => 1
    response.should redirect_to(new_session_url)
  end

end

describe PhotoSetsController, "when logged in as user other than the owner" do
  include PhotoSetsControllerSpecHelper

  before do
    stub_owner :can_be_edited_by? => false
    login
  end

  it "create should deny access" do
    post :create, :user_id => 1
    response.should be_forbidden
  end

end

describe PhotoSetsController, "create action, with invalid data" do
  include PhotoSetsControllerSpecHelper

  before do
    stub_tabs
    stub_owner :can_be_edited_by? => true
    login @owner
  end

  def make_request
    post :create, :user_id => 1
  end

  it_should_behave_like "user tab page"

  it "should redisplay form" do
    make_request
    response.should be_success
    response.should render_template('new')    
  end

end

describe PhotoSetsController, "create action" do
  include PhotoSetsControllerSpecHelper

  before do
    stub_tabs
    stub_owner :can_be_edited_by? => true
    login @owner

    @photo_set = mock_model(PhotoSet, :profile= => nil, :save => true)
    PhotoSet.stub!(:new).and_return(@photo_set)
  end

  def make_request
    post :create, :user_id => 1, :photo_set => valid_attributes
  end

  it_should_behave_like "user tab page"

  it "should create photo set from attributes" do
    PhotoSet.should_receive(:new).with(valid_attributes).and_return(@photo_set)
    @photo_set.should_receive(:save).and_return(true)    
    make_request
  end

  it "should set the photo sets profile to the owners profile" do
    @photo_set.should_receive(:profile=).with(@owner.profile)
    make_request
  end

  it "should redirect to index" do
    make_request
    response.should redirect_to(user_photo_set_url(@owner, @photo_set))
  end

  it "should assign the photo set to the view" do
    make_request
    assigns(:photo_set).should == @photo_set
  end

end

Using Test::Unit

class PhotoSetsControllerTest < Test::Unit::TestCase
  fixtures :users, :profiles, :photo_sets, :gallery_photos

  def test_that_create_requires_login
    assert_requires_login do |proxy|
      proxy.post :create, :user_id => 1003
    end
  end

  def test_that_create_requires_current_user_to_own_photo_set
    assert_requires_access(:pippin) do |proxy|
      proxy.post :create, :user_id => 1003
    end
  end

  def test_that_create_with_invalid_values_displays_new_template
    login_as :sam
    post :create, :user_id => 1003, :photo_set => invalid_photo_set

    assert_response :success
    assert_template 'new'
  end

  def test_that_create_creates_photo_set_and_redirects_photo_index
    login_as :sam
    post :create, :user_id => 1003, :photo_set => valid_photo_set

    photo_set = assigns(:photo_set)
    assert photo_set
    assert_equal 'Naked Shots', photo_set.title
    assert_equal profiles(:sam), photo_set.profile
    assert !photo_set.new_record?
    assert_equal 2, photo_set.position

    assert_redirected_to user_photo_set_path(users(:sam), photo_set)
  end

end