# Rspec approach to test model

**URL:** <https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074>\
**Category:** RSpec\
**Created:** [February 11, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074 "2009-02-11T14:51:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 11, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/1 "2009-02-11T14:51:37Z")

</div>

Hi,

I am quite new to Rspec. I want to use Rspec to test my existing Code. I  
start from Models (Unit Testing). Here i want your help on a issue.

Here is model/user\_spec.rb

describe User do  
before(:each) do  
@user=User.new  
@user.id=‘2’  
@user.email=‘removed\_email\_address@domain.invalid’  
@user.password=‘1234’  
@user.crypted\_password= ‘21066966a0578362e2115f3561bd4f144520ccd2’  
@user.salt= ‘574f09d3ae2473105567eab77ba9d3ae08ed40df’  
@user.remember\_token= ‘’  
@user.remember\_token\_expires\_at= ‘’  
@user.name= ‘kaleem’  
@user.company\_id=‘2’  
@user.title= ‘’  
@user.active= ‘1’  
@user.reset\_password\_token= ‘’  
end

it “should authenticate with valid email,password and approved  
company” do  
User.authenticate(@user.email, @user.password).should\_not be\_nil  
end

it “should NOT authenticate with INvalid email” do  
@user.email=nil  
User.authenticate(@user.email, @user.password).should be\_nil  
end

it “should NOT authenticate with INvalid password” do  
@user.password=nil  
User.authenticate(@user.email, @user.password).should be\_nil  
end

it “should remember me” do  
@user.remember\_me  
@user.remember\_token.should\_not be\_nil  
@user.remember\_token\_expires\_at.should\_not be\_nil  
end

it “Remember Token time should be less than Token expires time” do  
@user.remember\_token?.should be\_true  
end

it “should forget me” do  
@user.forget\_me  
@user.remember\_token.should be\_nil  
@user.remember\_token\_expires\_at.should be\_nil  
end  
end

Now Questions:

1. is my approach or way of writing specs is right???

2. How can i make specs for such a model action

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 11, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/2 "2009-02-11T17:58:57Z")

</div>

On 11/02/2009, at 8:51 AM, Kaleem U. wrote:

> @user=User.new  
> @user.active= ‘1’  
> User.authenticate(@user.email, @user.password).should be\_nil  
> @user.remember\_token\_expires\_at.should\_not be\_nil  
> end  
> Thank you 🙂  
> Hi Kaleem. All of that looks pretty good so far, though you shouldn’t  
> be setting the “id” attribute of an AR model:  
> @user.id = ‘2’

I suggest using the following format for creating your User object:

before(:each) do  
@user = User.new(  
:email =\> 'removed\_email\_address@domain.invalid’,  
:password =\> ‘1234’,  
# ..etc..  
)  
end

To spec your #activate! , why not do something like this?:

it ‘should activate the user’ do  
@user.active.should be\_false  
@user.activate!  
@user.active.should be\_true  
end

Cheers,  
Nick

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 11, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/3 "2009-02-11T18:01:56Z")

</div>

On Feb 11, 2009, at 8:51 AM, Kaleem U. wrote:

> @user=User.new  
> @user.active= ‘1’  
> @user.reset\_password\_token= ‘’  
> end

You could benefit from a factory / data builder. See  
FixtureReplacement, Fixjour, Factory girl, one of the many others out  
there which would build this stuff _once_ for you.

> end  
> end  
> end
> 
> Now Questions:
> 
> 1. is my approach or way of writing specs is right???

Remember: there is no right way of doing things. Does it work for you  
(and your team)? Is it clear?

> 1. How can i make specs for such a model action
> 
> def activate!  
> self.update\_attribute(:active, true)  
> end

spec1:  
@user.activate!  
@user.should be\_active

spec 2:  
@user.should\_not be\_active

Scott

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 11, 2009, 6:47pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/4 "2009-02-11T18:47:21Z")

</div>

Scott T. wrote:

> On Feb 11, 2009, at 8:51 AM, Kaleem U. wrote:
> 
> > @user=User.new  
> > @user.active= ‘1’  
> > @user.reset\_password\_token= ‘’  
> > end
> 
> You could benefit from a factory / data builder. See  
> FixtureReplacement, Fixjour, Factory girl, one of the many others out  
> there which would build this stuff _once_ for you.

Kaleem,

I recently installed nakajima-acts\_as\_fu (0.0.3) gem. This provides a  
rather painless way of specifying ActiveRecord model schemata on the fly  
in your specifications. You might find it helpful to look into this.

You will discover that some people favour mocks and stubs when dealing  
with AR models in tests/specifications and some favour hitting the  
database. Once you decide on your own preference in this matter the  
choice of approach is somewhat simplified.

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 11, 2009, 6:50pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/5 "2009-02-11T18:50:05Z")

</div>

Nick H. wrote:

> To spec your #activate! , why not do something like this?:
> 
> it ‘should activate the user’ do  
> @user.active.should be\_false  
> @user.activate!  
> @user.active.should be\_true  
> end

Thanks for your reply Hoffman 🙂

I did the same but it gives error like “#23000Duplicate entry  
'removed\_email\_address@domain.invalid Insert INTO (…) Values(…)”.  
It should do update not Insert.  
But when i try call it with a new instance of user (with no values  
assigned to any user attribute) then it works.  
i.e.  
it “should activate the user” do  
@user1=User.new  
@user1.activate!  
end  
This works.

I think i have to erase all the User attributes from “before (:each)”  
and only assigned it in Examples which required it. am i right ?

Thanks

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 11, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/6 "2009-02-11T18:57:19Z")

</div>

James B. wrote:

> I recently installed nakajima-acts\_as\_fu (0.0.3) gem. This provides a  
> rather painless way of specifying ActiveRecord model schemata on the fly  
> in your specifications. You might find it helpful to look into this.
> 
> You will discover that some people favour mocks and stubs when dealing  
> with AR models in tests/specifications and some favour hitting the  
> database. Once you decide on your own preference in this matter the  
> choice of approach is somewhat simplified.

Hi james byrne,

Good to know that.I will surely look for this gem…

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 11, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/7 "2009-02-11T19:40:52Z")

</div>

Scott T. wrote:

> On Feb 11, 2009, at 8:51 AM, Kaleem U. wrote:
> 
> > @user=User.new  
> > @user.active= ‘1’  
> > @user.reset\_password\_token= ‘’  
> > end
> 
> You could benefit from a factory / data builder. See  
> FixtureReplacement, Fixjour, Factory girl, one of the many others out  
> there which would build this stuff _once_ for you.

Hi Scott T.,

I am in need of such data builders.

So nice of you… 🙂

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 12, 2009, 8:15am UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/8 "2009-02-12T08:15:53Z")

</div>

Kaleem U. wrote:

> Scott T. wrote:

> > You could benefit from a factory / data builder. See  
> > FixtureReplacement, Fixjour, Factory girl, one of the many others out  
> > there which would build this stuff _once_ for you.

> I am in need of such data builders.

I recently thumped the square peg of FixtureDependencies into the round  
hole of  
Merb’s RSpec.

[GitHub - jeremyevans/fixture\_dependencies: Sequel/ActiveRecord fixture loader that handles dependency graphs · GitHub](http://github.com/jeremyevans/fixture_dependencies/tree/master)

I can answer questions about it if anyone wants to try it.

Except how to make it do rake db:fixtures:load. Does anyone know that  
one?

–  
Phlip

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 12, 2009, 8:21am UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/9 "2009-02-12T08:21:04Z")

</div>

Hi all,  
Below is my model action.

def activate!  
self.update\_attribute(:active, true)  
end

here is spec/models/user\_spec.rb

describe User do  
before(:each) do  
@user=User.new(:email=\>‘removed\_email\_address@domain.invalid’,:password=‘1234’…,)  
end

This is my spec.

it “should activate the user” do  
@user.activate!  
@user.active.should be\_true  
end

but it gives error like “#23000Duplicate entry  
'removed\_email\_address@domain.invalid Insert INTO (…) Values(…)”.  
It should do update not Insert.  
But when i try call it with a new instance of user (with no values  
assigned to any user attribute) then it works.  
i.e.  
it “should activate the user” do  
@user\_1=User.new  
@user\_1.activate!  
@user\_1.active.should be\_true  
end

I think i have to erase all the User attributes from “before (:each)”  
and only assigned it in Examples which required it.

am i right ?

Thanks

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 12, 2009, 11:35am UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/10 "2009-02-12T11:35:30Z")

</div>

kaleem,  
method new does not run your model validations, neither does your  
provided  
activate! implementation.  
but save indeed does.  
my guess is your mixing several contexts (cases, situations) in your  
single  
spec. Try decoupling all with more granular examples and you’ll get to  
save  
shore 🙂  
cheers,  
joaquin

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 12, 2009, 1:57pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/11 "2009-02-12T13:57:53Z")

</div>

Hi,

I am trying to write custom matcher which accepts block to spec views  
with forms/fieldsets/inputs eg

## view:
 Personal Information 
1. First name 
 ... 

## spec:

it “should have form with input fields” do  
render …  
response.should have\_form(users\_path) do  
with\_field\_set ‘Personal Information’ do  
with\_text\_field ‘First name’, ‘user[first\_name]’  
…  
end  
end  
end

## matches? of have\_form:

```
def matches?(response, &block)
  @block = block if block
  response.should @scope.have_tag('form[action=?]', @action) do
    @block.call.matches?(response)
  end
end matches? of have_field_set: ---

def matches?(response, &block)

  @block = block if block
  response.should @scope.with_tag('fieldset') do
    @scope.with_tag('legend', @legend) if @legend
    @scope.with_tag('ol') do
      @block.call.matches?(response)
    end
  end
end but I get: NoMethodError in '/users/new should have form with

```

input fields’ undefined method `matches?' for true:TrueClass ./spec/views/users/../../custom_ui_matchers/with_field_set.rb:10:in `matches?’  
./spec/views/users/../../custom\_ui\_matchers/have\_form.rb:11:in  
`matches?' ./spec/views/users/../../custom_ui_matchers/have_form.rb:10:in `matches?’ ./spec/views/users/new.html.haml\_spec.rb:17:  
/usr/lib64/ruby/1.8/timeout.rb:53:in `timeout’ something wrong with  
yielding block? Gist: [http://gist.github.com/62562](http://gist.github.com/62562) Thanks in advance,  
Yury

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 12, 2009, 3:57pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/12 "2009-02-12T15:57:37Z")

</div>

On Thu, Feb 12, 2009 at 5:52 AM, Yury K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ---
> 
> def matches?(response, &block)  
> ./spec/views/users/../../custom\_ui\_matchers/with\_field\_set.rb:10:in  
> `matches?' ./spec/views/users/../../custom_ui_matchers/have_form.rb:11:in `matches?’ ./spec/views/users/../../custom\_ui\_matchers/have\_form.rb:10:in  
> `matches?' ./spec/views/users/new.html.haml_spec.rb:17: /usr/lib64/ruby/1.8/timeout.rb:53:in `timeout’ something wrong with yielding  
> block? Gist: [http://gist.github.com/62562](http://gist.github.com/62562) Thanks in advance, Yury

What does initialize look like?

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 12, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/13 "2009-02-12T18:14:23Z")

</div>

David C. wrote:

> > end  
> > end matches? of have\_field\_set: —  
> > end but I get: NoMethodError in '/users/new should have form with input

initialize for have\_form:

class HaveForm def initialize(action, scope, &block)  
@action, @scope, @block = action, scope, block  
end … end

def have\_form(action, &block)  
HaveForm.new(action, self, &block)  
end

initialize for have\_field\_set:

class WithFieldSet  
def initialize(scope, legend = nil, &block)  
@scope, @legend, @block = scope, legend, block  
end  
end

def with\_field\_set(legend = nil, &block)  
WithFieldSet.new(self, legend, &block)  
end

btw I put it on gist: [http://gist.github.com/62562](http://gist.github.com/62562)

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 13, 2009, 12:11am UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/14 "2009-02-13T00:11:06Z")

</div>

SOLVED! just need to pass matched result to inner block. gist:  
[http://gist.github.com/62562](http://gist.github.com/62562)

Many thanks,  
Yury

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 13, 2009, 5:04am UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/15 "2009-02-13T05:04:48Z")

</div>

Yury K. wrote:

> SOLVED! just need to pass matched result to inner block. gist:  
> [http://gist.github.com/62562](http://gist.github.com/62562)
> 
> Many thanks,  
> Yury

I was wrong. It yields just one last line from the inner block. It  
passes following:

it “should have form to create a new user” do  
render ‘/users/new’  
response.should have\_form(users\_path) do  
with\_field\_set ‘Personal Information’ do  
with\_text\_field ‘wrong’, ‘wrong’ # there is no such field  
with\_text\_field ‘First Name’, ‘user[first\_name]’  
end  
end  
end gist: [http://gist.github.com/62562](http://gist.github.com/62562) TIA, Yury

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 13, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/16 "2009-02-13T21:01:01Z")

</div>

Yury K. wrote:

> it “should have form with input fields” do  
> render …  
> response.should have\_form(users\_path) do  
> with\_field\_set ‘Personal Information’ do  
> with\_text\_field ‘First name’, ‘user[first\_name]’  
> …  
> end  
> end  
> end

The minor problem with that system is it forces your test to say exactly  
what  
the code says. That’s not “driven” development!

If you can forbear to use matchers (shocked gasp!), at my day-job we  
match  
blocks all the time with assert2’s new xpath system:

require ‘assert2/xpath’

assert\_xhtml response

xpath :form, :action =\> users\_path do  
xpath :fieldset, ?. =\> ‘Personal Information’ do  
xpath :input, :type =\> ‘text’, :name =\> ‘user[first\_name]’ and  
xpath :input, :type =\> ‘text’, :name =\> ‘user[last\_name]’  
end  
end

From there, wrapping the xpath() calls up into kewt with\_text\_field()  
macros  
would be trivial. They could also absolves the redundant ‘user’ text  
on the  
names, for example.

If any inner xpath() fails, there, the fault diagnostic contains a  
formatted &  
indented copy of the HTML block under inspection. The entire page would  
not spew  
out! Only the or would.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 13, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/17 "2009-02-13T21:17:29Z")

</div>

On Fri, Feb 13, 2009 at 1:53 PM, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> 
> xpath :form, :action =\> users\_path do  
> xpath :fieldset, ?. =\> ‘Personal Information’ do  
> xpath :input, :type =\> ‘text’, :name =\> ‘user[first\_name]’ and  
> xpath :input, :type =\> ‘text’, :name =\> ‘user[last\_name]’  
> end  
> end

Hey Philip,

This looks pretty cool. I wonder if you’d have any interest in making  
this a bit more rspec-friendly? Something like an option to run it  
like this:

expect\_xpath do  
xpath :form, :action =\> users\_path do  
xpath :fieldset, ?. =\> ‘Personal Information’ do  
xpath :input, :type =\> ‘text’, :name =\> ‘user[first\_name]’ and  
xpath :input, :type =\> ‘text’, :name =\> ‘user[last\_name]’  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 14, 2009, 1:06am UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/18 "2009-02-14T01:06:12Z")

</div>

David C. wrote:

> This looks pretty cool. I wonder if you’d have any interest in making  
> this a bit more rspec-friendly? Something like an option to run it  
> like this:
> 
> expect\_xpath do

It’s on my do-list, but…

…are pluggable matchers as hard to write as the OP implied? How would  
you fix  
his problem? I need to know that before diving into the spaghetti that  
Ruby  
inevitably generates, just below every kewt DSL!

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 14, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/19 "2009-02-14T22:34:19Z")

</div>

Phlip wrote:

> …are pluggable matchers as hard to write as the OP implied? How  
> would you fix his problem? I need to know that before diving into the  
> spaghetti that Ruby inevitably generates, just below every kewt DSL!

The major problem is fixed:  
[http://github.com/yura/howto-rspec-custom-matchers/](http://github.com/yura/howto-rspec-custom-matchers/)

But there are still few open questions in README.rdoc

Regards,  
Yury

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kaleemullah](https://www.ruby-forum.com/u/kaleemullah)\
**Post date:** [February 14, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074/20 "2009-02-14T22:41:23Z")

</div>

Yury K. wrote:

> The major problem is fixed:  
> [http://github.com/yura/howto-rspec-custom-matchers/](http://github.com/yura/howto-rspec-custom-matchers/)

Apologies if I missed something, but is that a howto or a library? Do I  
git it  
and run it to … learn to customize matchers?

[Next page](https://www.ruby-forum.com/t/rspec-approach-to-test-model/160074.md?page=2)
