# Rspec tutorial

**URL:** <https://www.ruby-forum.com/t/rspec-tutorial/111332>\
**Category:** Rails\
**Created:** [August 6, 2007, 5:03am UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332 "2007-08-06T05:03:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![oren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecd19e/32.png) [@oren](https://www.ruby-forum.com/u/oren)\
**Post date:** [August 6, 2007, 5:03am UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332/1 "2007-08-06T05:03:10Z")

</div>

I am trying to follow this tutorial -  
[http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1](http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1)

I installed the Rspec gem but I can’t install the RSpec On Rails  
plugin (the url is not goood).

1. the URL for the RSpec On Rails plugin is not good so I can’t  
install it.

2. What is the usage of this plugin?  
I think that it let me generate Rspec files like this: script/generate  
rspec\_model User  
but I am not sure.

3. What is the different between a gem and a plugin?  
here is my guess:  
a gem is global to my machine and a plugin is added to a specific  
project.  
a plugin adds a generator to my project.

---

<div class="post-metadata">

**Author:** ![oren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecd19e/32.png) [@oren](https://www.ruby-forum.com/u/oren)\
**Post date:** [August 6, 2007, 5:11am UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332/2 "2007-08-06T05:11:10Z")

</div>

On 8/5/07, oren [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to follow this tutorial -  
> [http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1](http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1)
> 
> I installed the Rspec gem but I can’t install the RSpec On Rails  
> plugin (the url is not goood).
> 
> 1. the URL for the RSpec On Rails plugin is not good so I can’t  
> install it.

See [http://rspec.rubyforge.org/documentation/rails/install.html](http://rspec.rubyforge.org/documentation/rails/install.html) for  
current information.

---

<div class="post-metadata">

**Author:** ![oren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecd19e/32.png) [@oren](https://www.ruby-forum.com/u/oren)\
**Post date:** [August 6, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332/3 "2007-08-06T20:47:10Z")

</div>

great. it works.  
now i am facing this problem-  
I get this error when running Rspec-  
NoMethodError in ‘A user (in general) should be invalid without a  
username’  
undefined method `should\_not\_be\_valid’ for #User:0xb6fdbd4c  
./spec/models/user\_spec.rb:9:

#the model - user.rb  
class User \< ActiveRecord::Base  
validates\_presence\_of :username  
end

#the migration file - 001\_create\_users.rb  
this is my migration file:  
class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table :users do |t|  
t.column :first\_name, :string  
t.column :last\_name, :string  
t.column :email, :string  
t.column :description, :string  
t.column :username, :string  
t.column :encrypted\_password, :string  
t.column :salt, :string  
end  
end

def self.down  
drop\_table :users  
end  
end

#the rpec file - user\_spec.rb  
require File.dirname( **FILE** ) + ‘/…/spec\_helper’

context “A user (in general)” do  
setup do  
@user = User.new  
end

specify “should be invalid without a username” do  
@user.should\_not\_be\_valid  
end  
end

---

<div class="post-metadata">

**Author:** ![oren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecd19e/32.png) [@oren](https://www.ruby-forum.com/u/oren)\
**Post date:** [August 8, 2007, 8:07am UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332/4 "2007-08-08T08:07:53Z")

</div>

thanks David, it works.  
and I also found a nice notification popup for my ubuntu:  
[http://snakesgemscoffee.blogspot.com/2007/07/marrying-autotest-with-rspec-on-gnome.html](http://snakesgemscoffee.blogspot.com/2007/07/marrying-autotest-with-rspec-on-gnome.html)

---

<div class="post-metadata">

**Author:** ![oren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecd19e/32.png) [@oren](https://www.ruby-forum.com/u/oren)\
**Post date:** [August 6, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332/5 "2007-08-06T21:00:12Z")

</div>

On 8/6/07, oren [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> great. it works.  
> now i am facing this problem-  
> I get this error when running Rspec-  
> NoMethodError in ‘A user (in general) should be invalid without a  
> username’  
> undefined method `should\_not\_be\_valid’ for #User:0xb6fdbd4c  
> ./spec/models/user\_spec.rb:9:

You must reading some old docs. Try this:

user.should\_not be\_valid

(no \_ between not and be)

---

<div class="post-metadata">

**Author:** ![oren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecd19e/32.png) [@oren](https://www.ruby-forum.com/u/oren)\
**Post date:** [August 16, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332/6 "2007-08-16T21:25:47Z")

</div>

David C. wrote:

> On 8/6/07, oren [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > great. it works.  
> > now i am facing this problem-  
> > I get this error when running Rspec-  
> > NoMethodError in ‘A user (in general) should be invalid without a  
> > username’  
> > undefined method `should\_not\_be\_valid’ for #User:0xb6fdbd4c  
> > ./spec/models/user\_spec.rb:9:
> 
> You must reading some old docs. Try this:
> 
> user.should\_not be\_valid
> 
> (no \_ between not and be)

David, I’m working through the same tutorial and can’t get part of the  
spec to fail. There’s probably a simple explanation for it, but it  
doesn’t make much sense to me at the moment. I tried changing the error  
message checked for and it had not affect. It should have failed.  
Here’s the code:

################################################################  
class User \< ActiveRecord::Base  
validates\_presence\_of :username , :message =\> “is required”  
end  
################################################################  
require File.dirname( **FILE** ) + ‘/…/spec\_helper’

describe User do  
before do  
@user = User.new  
end

it “should be invalid without a username” do  
@user.should\_not be\_valid  
@user.errors.on(:username).should equal?(“is _this\_should\_fail_  
required”)  
@user.username = ‘someusername’  
@user.should be\_valid  
end

after do  
@user = nil  
end  
end  
################################################################

Does anyone know what’s missing?

---

<div class="post-metadata">

**Author:** ![oren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecd19e/32.png) [@oren](https://www.ruby-forum.com/u/oren)\
**Post date:** [August 16, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332/7 "2007-08-16T22:43:43Z")

</div>

David C. wrote:

> Get rid of the question mark:
> 
> @user.errors.on(:username).should equal(“is \_this\_should\_fail\_required”)
> 
> Also - keep in mind that rspec has 3 equality matchers:
> 
> equal(expected)  
> eql(expected)  
> == expected
> 
> Each one works like the corresponding ruby method. So …
> 
> “this”.should equal(“this”)
> 
> will fail, because “this”.equal?(“this”) would fail.

Thank you Sir. That did the trick.

---

<div class="post-metadata">

**Author:** ![oren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecd19e/32.png) [@oren](https://www.ruby-forum.com/u/oren)\
**Post date:** [August 16, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/rspec-tutorial/111332/8 "2007-08-16T22:14:06Z")

</div>

On 8/16/07, Cody S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ./spec/models/user\_spec.rb:9:  
> > > message checked for and it had not affect. It should have failed.  
> > > before do  
> > > @user = User.new  
> > > end
> 
> it “should be invalid without a username” do  
> @user.should\_not be\_valid  
> @user.errors.on(:username).should equal?(“is _this\_should\_fail_  
> required”)

Get rid of the question mark:

@user.errors.on(:username).should equal(“is \_this\_should\_fail\_required”)

Also - keep in mind that rspec has 3 equality matchers:

equal(expected)  
eql(expected)  
== expected

Each one works like the corresponding ruby method. So …

“this”.should equal(“this”)

will fail, because “this”.equal?(“this”) would fail.
