Skip to content

Instantly share code, notes, and snippets.

@tankwanghow
Created May 15, 2010 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tankwanghow/402517 to your computer and use it in GitHub Desktop.
Save tankwanghow/402517 to your computer and use it in GitHub Desktop.
class SessionsController < ApplicationController
skip_before_filter :login_required
def create
user = User.authenticate(params[:username], params[:password])
if user
session[:user_id] = user.id
flash[:notice] = "Logged in successfully."
redirect_to_target_or_default(twikets_path)
else
flash.now[:error] = "Invalid username or password!"
render :action => 'new'
end
end
end
require 'spec_helper'
describe SessionsController do
let(:user) { User.make(:username => 'bob') }
describe '#create' do
context 'authenticate fail' do
before(:each) { User.stubs(:authenticate).returns(nil) }
it 'session[:user_id] should == user.id' do
post :create
session[:user_id].should be_nil
end
it 'should render_template :new' do
post :create
response.should render_template :new
end
#############
# Failing Spec
##################################################################
it 'flash.now[:error] should be set to failure' do
post :create
flash.now[:error].should == "Invalid username or password!"
end
##################################################################
end
context 'authenticate successfully' do
before(:each) { User.stubs(:authenticate).returns(user) }
#############
# Failing Spec
##################################################################
it 'should redirect' do
expects(:redirect_to_target_or_default).with(twikets_path)
post :create
end
##################################################################
it 'session[:user_id] should == user.id' do
post :create
session[:user_id].should == user.id
end
it 'flash[:notice] should be set to success' do
post :create
flash[:notice].should == "Logged in successfully."
end
end
end
end
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(Rails)
require 'rspec/rails'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
Remarkable.include_matchers!(Remarkable::ActiveRecord, Rspec::Core::ExampleGroup)
Remarkable.include_matchers!(Remarkable::ActiveModel, Rspec::Core::ExampleGroup)
Rspec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# config.mock_with :rspec
# If you'd prefer not to run each of your examples within a transaction,
# uncomment the following line.
# config.use_transactional_examples = false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment