zoukankan      html  css  js  c++  java
  • [RSpec] LEVEL 1: INTRODUCTION

    Install RSpec:

    Describe

    Lets start writing a specification for the Tweet class. Write a describe block for the Tweet model without any examples inside it.

    #tweet.rb
    
    
    class Tweet
    end

    Answer:

    #tweet_spec.rb
    
    describe Tweet do
    
    end

    It

    Now create a pending test called "can set status".

    describe Tweet do
      it "can set status"
      
    end

    Couple of ways to make test pending:

    Expectation

    Now let's write the example. Go ahead and instantiate a tweet, give it the status "Nom nom nom", and test the status has been properly set to this value using an == equality matcher.

    class Tweet
      attr_accessor :status
     
      def initialize(options={})
        self.status = options[:status]
      end
     
      def public?
        self.status && self.status[0] != "@"                                 
      end
    end

    Answer:

    describe Tweet do
      it 'can set status' do
        tweet = Tweet.new
        tweet.status = "Nom nom nom"
        tweet.status.should == "Nom nom nom"
      end
    end

    Matchers

    Using a predicate 'be' matcher, make sure that a tweet like "Nom nom nom" (which is not a reply because it doesn't start with an '@' sign) is public.

    describe Tweet do
      it 'without a leading @ symbol should be public' do
        tweet = Tweet.new(status: 'Nom nom nom')
        tweet.should be_public
      end
    end

    Comparison Matchers

    Finish the example below to ensure that our tweet.status.length is less than or equal to 140 characters. Use a be matcher in your spec.

    class Tweet
      attr_accessor :status
     
      def initialize(options={})
        self.status = options[:status]
      end
     
      def public?
        self.status && self.status[0] != "@"                                 
      end
     
      def status=(status)
        @status = status ? status[0...140] : status
      end
    end

    Answer:

    describe Tweet do
      it 'truncates the status to 140 characters' do
        tweet = Tweet.new(status: 'Nom nom nom' * 100)
        tweet.status.length.should be <= 140
      end
    end

    Predict 'be':

  • 相关阅读:
    【BZOJ4568】幸运数字(SCOI2016)-树上倍增+异或线性基合并
    【BZOJ2115】XOR(WC2011)-异或线性基+DFS树+贪心
    【HDU3949】XOR-异或线性基
    【BZOJ3105】新Nim游戏(CQOI2013)-博弈论+异或线性基+贪心
    购物单 && 动态规划 && 背包问题
    求int型正整数在内存中存储时1的个数 && int型的数到底最大是多少?
    Eqaulize Prices
    Nearest Interesting Number
    英文句子的逆序
    字符串反转
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4098355.html
Copyright © 2011-2022 走看看