• Rspec基本语法


    引用链接:http://reverocean.iteye.com/blog/1489957

    1. describe和context
    describe和context方法用来组织相关的行为example。
    使用一个字符串作为他们的参数,以及使用一个block来定义其上下文的范围。
    写model的spec或者其他的unit test时,可以传一个Ruby类作为describe的第一个参数。Doing so also creates an implicit subject for the examples.
    describe Timesheet do
    ……
    end
    describe可以嵌套
    一般使用:
    describe Timesheet do
    describe “#test” do
    …..
    end
    end
    这样的方式(注意第一个describe的参数是一个类,第二个describe参数是以#开始)这个表示测试Timesheet类下面的test方法)
    2. let(:name) {expression}
    let方法简单的使用后面的block创建memoized attributes.换句话说就是为后面的测试准备数据,跟before里的代码一样的,但是比before里的代码效果更好。
    memozied的意思是let后面关联的block只执行一次,然后会缓存该变量,提高执行效率。
    Lazy,有需要才会运算,并且是Memoized
    相较于before(:each)增加执行速度
    不需要用instance variable放before
    增加可读性
    let!则是非lazy
    3. before和after
    和setup、teardown方法类似
    Before and after code can be inserted in any describe or context blocks, and by default the execute for each it block that shares their scope.
    4. it
    it方法使用一个描述和block。一个it就是一个测试,最好一个it一个期望
    As mentioned, the idea is to complete the thought that was started in the describe method, so that it foms a complete sentence.
    5. specify
    specify是it方法的别名,但是他可以使用不同的结构来增加可读性。
    describe BlogPost do
    set(:blog_post) {blog_post = BlogPost.new}
    specify {blog_post.should_not be_published}
    end
    生成的RSpecDoc如下:
    BlogPost
    - should not be published
    6. expect
    expect 用来改变一个值或者抛出一个异常。后面接change来表示要达到的值,使用raise_error(异常类)来表示会抛出一个异常。
    expect {
    BlogPost.create :title => “Hello”
    }.to change {BlogPost.count}.by(1)
    希望在expect块里做完之后,BlogPost.count的值要改为1
    改变值的例子
    describe Order do
      let(:order) {order = Order.create}
      describe "#ship!” do
        context “with paid” do
          it "should update status to shipping" do
            expect {
              order.ship!
              }.to change { order.status }.from(“new”).to(“ship”)
           end
        end
    end
    这里改变值使用的from和to,这样就会在执行expect块之前检查order.status的值是不是new,并且会在执行之后检查是不是”ship”值
    抛出异常的例子
    describe Order do
      let(:order) {order = Order.create}
      describe "#ship!” do
        context “with paid” do
          it "should raise NotPaidError" do
            expect {
              order.paid? = flase
              order.ship!
              }.to raise_error(NotPaidError)
           end
        end
    end
    这里表示执行完except块之后会抛出一个NotPaidError异常。

    7. pending
    可以使用pending来列出打算要写的测试
    使用it函数不传block给他也是pending的意思,也可以在block离调用pending

    可以在before里写pending
    8. should和should_not
    Rspec mixes them into the base Ruby Object class at runtime so that they are available on all objects.They expect to receive Matcher objects, generated using Rspec expectation syntax
    receiver.should be_true
    receiver.should be_false
    receiver.should be_nil
    检查型别、方法
    receiver.should be_a_kind_of(Array)
    receiver.should be_an_instance_of(Array)
    receiver.should responsed_to(:foo)
    检查Array、Hash
    receiver.should have_key(:foo)
    receiver.should include(4)
    receiver.should have(3).items
    任何be_开头
    receiver.should be_empty
    receiver.should be_blank
    receiver.should be_admin
    should == 是万能的
    Rspec的Matcher很多,也可以自己写
    9. Implicit Subject和Explicit Subject
    使用subject可省略receiver
    10. its
    its可以省略receiver的方法调用
    describe Order do
    subject { Order.new}
    its(:status) {should == “New”}
    end

  • 相关阅读:
    区块链入门
    上海美食餐厅
    《OD学hadoop》20160910某旅游网项目实战
    《OD学hadoop》20160904某旅游网项目实战
    《OD学hadoop》20160903某旅游网项目实战
    qt5-自定义类
    qt5-Qt Creator使用
    qt5-QPushButton按钮
    qt5-工程文件的介绍--快捷键
    电路分析-电阻
  • 原文地址:https://www.cnblogs.com/dami520/p/3262047.html
Copyright © 2020-2023  润新知