How Can I inveke a code example in another one

Hi,
I’m new to rspec.
How could I transform the Unit:Tests code like

def test_logout
login
get :logout
assert_not_logged_in
end
def assert_not_logged_in
assert_block { session[“user”].nil? }
end

that a test method invoke another test method,to repec code.

Should I put them in one code example? or there is other way?

Thanks!

On Oct 28, 2009, at 2:56 AM, zhjie685 wrote:

assert_block { session["user"].nil? }

end

that a test method invoke another test method,to repec code.

Should I put them in one code example? or there is other way?

Thanks!

Instead of assertions, RSpec uses expectations, like this:

describe “GET /logout” do
it “removes the user from the session” do
login
get :logout
session[“user”].should be_nil
end
end

An expectation has two parts: should() or should_not(), and a matcher.
In this case, be_nil() is a matcher that’s built in to RSpec. You can
also write your own, using RSpec’s matcher DSL:

Spec::Matchers.define :be_logged_out do
match do |session|
session[“user”].nil?
end
end

describe “GET /logout” do
it “ends the user session” do
login
get :logout
session.should be_logged_out
end
end

Or you could write a be_logged_in matcher and use it with should_not,
like this:

Spec::Matchers.define :be_logged_in do
match do |session|
!!session[“user”]
end
end

describe “GET /logout” do
it “ends the user session” do
login
get :logout
session.should_not be_logged_in
end
end

Check out the following for more info:

http://wiki.github.com/dchelimsky/rspec/custom-matchers
http://github.com/dchelimsky/rspec/tree/master/features/matchers/

and, of course:

http://www.pragprog.com/titles/achbd/the-rspec-book

HTH,
David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs