2nd Nov, 2007

RSpec进阶指南(2): 基础知识

下面是RSpec测试的一些基础语法,这些都是会运行通过的测试例:

Strings:

'foo'.should == 'foo'
'foo'.should === 'foo'
'foo'.should_not equal('foo')
''.should be_empty
'foo with bar'.should include('with')
'http://fr.ivolo.us'.should match(/http:\/\/.+/i)
nil.should be_nil

Numbers:

100.should < 200
200.should >= 100
(200 - 100).should == 100

# (100 - 80) is less than 21
100.should be_close(80,21)

Arrays:

[1,2,3].should have(3).items
[].should be_empty
[1,2,3].should include(2)

Hashes:

{}.should be_empty
{:post => {:title => 'test'}}.should have_key(:post)
{:post => {:title => 'test'}}.should_not have_key(:title)
false.should be_false
true.should be_true

Records:

# assuming @post = Post.new(:title => 'test')
@post.should be_instance_of(Post)
@post.should respond_to(:title)

相关文章:

评论

留条评论?

Your response:

Categories