# Newbie : How to spec params hash amendment

**URL:** <https://www.ruby-forum.com/t/newbie-how-to-spec-params-hash-amendment/192449>\
**Category:** RSpec\
**Created:** [July 30, 2010, 1:11pm UTC](https://www.ruby-forum.com/t/newbie-how-to-spec-params-hash-amendment/192449 "2010-07-30T13:11:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [July 30, 2010, 1:11pm UTC](https://www.ruby-forum.com/t/newbie-how-to-spec-params-hash-amendment/192449/1 "2010-07-30T13:11:16Z")

</div>

I am new to rspec and am finding it all a bit daunting at the moment.  
I’d like to be able to adopt a bdd approach to my development but I’m  
still at the stage of trying to get my head around rspec concepts so  
I’m committing the cardinal sin of coding first and then writing  
tests.

I am using the following:  
Ruby v1.8.7  
Rails v2.3.8  
Authlogic v2.1.5  
Cancan v1.2.0

rspec v1.3.0  
rspec-rails v1.3.2  
webrat v0.7.1  
cucumber-rails v0.3.2  
capybara v0.3.9  
factory\_girl v1.2.4  
pickle v0.3.0  
no\_peeping\_toms v1.1.0  
email\_spec v0.6.2

In spec\_helper.rb, I have  
def current\_user(stubs = {})  
@current\_user ||= mock\_model(User, stubs)  
end

def user\_session(stubs = {}, user\_stubs = {})  
@current\_user\_session ||= mock\_model(UserSession, {:user =\>  
current\_user(user\_stubs)}.merge(stubs))  
end

def login(session\_stubs = {}, user\_stubs = {})  
UserSession.stub!(:find).and\_return(user\_session(session\_stubs,  
user\_stubs))  
end

def logout  
@user\_session = nil  
end

def login\_as\_admin  
login({}, {:role =\> ‘admin’})  
@current\_user.stub(:role?).with(:admin).and\_return(true)  
end

In ability.rb (for cancan)  
def initialize(user)  
user ||= User.new # Guest user  
if user.role? :admin  
can [:index, :show, :new, :edit, :create, :update, :destroy],  
[User, CommonContents, Logo]  
else  
can [:show, :edit, :update], User  
cannot [:index, :show, :new, :edit, :create, :update, :destroy],  
[CommonContents, Logo]  
end  
end

In UserController  
def create

# New users can only be created by admins

authorize! :create, User

# When the user is first defined by admin, the password is not set

# Create a random one to satisfy validation requirements, which the

user changes when they first visit  
params[:user][:password] = random\_password()  
params[:user][:password\_confirmation] = params[:user][:password]

@user = User.new(params[:user])

if @user.save  
redirect\_to(@user, :notice =\> ‘User was successfully  
created.’)  
else  
@roles = User.const\_get(“ADMIN\_CAN\_CREATE”)  
render :action =\> “new”  
end  
end

I have a private method in ApplicationController as follows:  
def random\_password(length = 10)  
alphanumerics = [(‘0’…‘9’),(‘A’…‘Z’),(‘a’…‘z’)].map {|range|  
range.to\_a}.flatten  
(0…length).map  
{ alphanumerics[Kernel.rand(alphanumerics.size)] }.join  
end

My spec is currently  
describe “when authorised as admin” do  
describe “POST ‘create’” do  
before(:each) do  
login\_as\_admin  
end  
after(:each) do  
logout  
end  
describe “with valid params” do  
it “should set password and password\_confirmation to the same 10  
digit value” do  
# ???  
end  
end  
end  
end

I have no idea how to approach 'rspec’ing the creation of the  
params[:user][:password] and :password\_confirmation or the  
random\_password method and would very much appreciate it if somebody  
could spare the time to suggest what I should be doing.

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [August 2, 2010, 4:03pm UTC](https://www.ruby-forum.com/t/newbie-how-to-spec-params-hash-amendment/192449/2 "2010-08-02T16:03:04Z")

</div>

On 7/30/10 5:53 AM, Martin H. wrote:

> I have no idea how to approach 'rspec’ing the creation of the  
> params[:user][:password] and :password\_confirmation or the  
> random\_password method and would very much appreciate it if somebody  
> could spare the time to suggest what I should be doing.  
> You could stub the random\_password method with some value, then perform  
> the create action, then confirm that the @user variable has that value  
> for password and password\_confirmation. (I forget if instance variables  
> are accessible in a controller test even after a redirect, so I guess  
> you’ll find out.)

– Elliot
