Skip to content

Instantly share code, notes, and snippets.

@kibyegn
Created March 24, 2011 07:18
Show Gist options
  • Save kibyegn/884698 to your computer and use it in GitHub Desktop.
Save kibyegn/884698 to your computer and use it in GitHub Desktop.
Devise functional testing with rspec
#account_configurations_controller_spec.rb
require 'spec_helper'
describe AccountConfigurationsController do
include Devise::TestHelpers
before (:each) do
@user = Factory(:user)
@user.confirm!
sign_in :user, @user
end
...
it "should be able to update the ac_name on an account_configuration" do
account_configuration = Factory(:account_configuration, :ac_name => "Foo", :user_id => @user.id)
post :update, :id => account_configuration.id, :user_id => @user.id, :account_configuration => {:ac_name => "Bar"}
response.should redirect_to(user_account_configuration_path(@user, account_configuration.id))
updated_account_configuration = AccountConfiguration.find(account_configuration.id)
updated_account_configuration.ac_name.should == "Bar"
end
#factories.rb
Factory.define :user do |u|
u.email { Factory.next(:email) }
u.password 'password'
u.password_confirmation 'password'
end
Factory.define :account_configuration do |u|
u.ac_name 'Foo'
u.association :user, :factory => :user
end
#app/models/user.rb
class User < ActiveRecord::Base
after_create :build_account_configuration
has_one :account_configuration, :dependent => :destroy
...
protected
def build_account_configuration
self.create_account_configuration(:user_id => self.id)
true
end
end
#app/models/account_configuration.rb
class AccountConfiguration < ActiveRecord::Base
belongs_to :user
end
#app/controllers/account_configurations_controller.rb
class AccountConfigurationsController < ApplicationController
def update
@account_configuration = current_user.account_configuration
@user = current_user
respond_to do |format|
if @account_configuration.update_attributes(params[:account_configuration])
format.html { redirect_to( user_account_configuration_path(@user, @account_configuration), :notice => 'Configurations were successfully updated.') }
else
render :action => :edit
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment