1 对象.should(_not) be_方法 :应该(不应该)怎么样,方法的返回值为true/false describe User do it "should be in any roles assigned to it" do user = User.new user.assign_role("assigned role") user.should be_in_role("assigned role") end
it "should not be in any roles unassigned to it" do user = User.new user.assign_role("assigned role") user.should_not be_in_role("unassigned role") end
end
2 before/after(:each/:all) do end before(:all) do # 会在所有example运行前被调用一次 end before do # 与before(:each)相同,会在每个example运行前被调用一次 end after(:each) do # 会在每个example运行完后被调用一次 @post.destroy unless @post.new_record? end after(:all) do # 会在所有examples运行完之后被调用一次 Post.destroy_all end
实例: describe Post do before(:each) do @post = Post.new(valid_post_hash) # grabs the hash below end it "should be valid" do @post.should be_valid end it "should not be valid without a title" do @post.title = '' @post.should_not be_valid end it "should not be valid without a body" do @post.body = '' @post.should_not be_valid end def valid_post_hash {:title => 'test', :body => 'test body'} end end
[code="rspec"]1 对象.should(_not) be_方法 :应该(不应该)怎么样,方法的返回值为true/falsedescribe User do it "should be in any roles assigned to it" do user = User.new user.assign_role("assigned role") use...