RubyRuby & Rails

rspec 测试书写规范

2017-10-26  本文已影响18人  云莉6

之前很为写测试头痛,从现在开始测试也要认真写啊 😂 。
总是搞不懂 describe context it let subject before/after 之间先后顺序以及含义。大概看了一下 rspec-style-guide,可以有一定的了解。本文摘自 The RSpec Style Guide,详尽的请参考原文。

完整结构:

class

class Article
  def summary
    #...
  end

  def self.latest
    #...
  end
end  

article_spec.rb

describe Article do
  subject { FactoryGirl.create(:some_article) }
  let(:user) { FactoryGirl.create(:user) }
  
  before do
    # ...
  end

  after do
    # ...
  end
  
  describe '#summary' do
    context 'when there is a summary' do
      it 'returns the summary' do
        # ...
      end
    end
  end
  
  describe '.latest' do
    context 'when latest' do
      it 'returns the latest data' do
        # ...
      end
    end
  end
end

articles_controller_spec.rb

# A classic example for use of contexts in a controller spec is creation or update when the object saves successfully or not.

describe ArticlesController do
  let(:article) { double(Article) }

  describe 'POST create' do
    before { allow(Article).to receive(:new).and_return(article) }

    it 'creates a new article with the given attributes' do
      expect(Article).to receive(:new).with(title: 'The New Article Title').and_return(article)
      post :create, article: { title: 'The New Article Title' }
    end

    it 'saves the article' do
      expect(article).to receive(:save)
      post :create
    end

    context 'when the article saves successfully' do
      before do
        allow(article).to receive(:save).and_return(true)
      end

      it 'sets a flash[:notice] message' do
        post :create
        expect(flash[:notice]).to eq('The article was saved successfully.')
      end

      it 'redirects to the Articles index' do
        post :create
        expect(response).to redirect_to(action: 'index')
      end
    end

    context 'when the article fails to save' do
      before do
        allow(article).to receive(:save).and_return(false)
      end

      it 'assigns @article' do
        post :create
        expect(assigns[:article]).to eq(article)
      end

      it 're-renders the 'new' template' do
        post :create
        expect(response).to render_template('new')
      end
    end
  end
end
上一篇 下一篇

猜你喜欢

热点阅读