美文网首页RubyRuby & Rails
rspec 测试书写规范

rspec 测试书写规范

作者: 云莉6 | 来源:发表于2017-10-26 17:57 被阅读18次

之前很为写测试头痛,从现在开始测试也要认真写啊 😂 。
总是搞不懂 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

相关文章

  • rspec 测试书写规范

    之前很为写测试头痛,从现在开始测试也要认真写啊 ? 。总是搞不懂 describe context it let ...

  • rspec

    没有测试的代码就是耍流氓 rspec入门教程不错rspec文档 rspec(1) - summary specs(...

  • RSpec Expectation And change mat

    RSpec Expectation 被用来描述期望的输出,测试中表明“描述被测试代码行为”。在 RSpec 中,期...

  • Everyday-rails-rspec - 安装Rspec

    安装rspec 项目中使用rspec,首先安装rspec。在Gemfile中添加下面的gem依赖。 创建测试数据库...

  • ruby on rails 集成测试-1

    这里,我的集成测试用的是capybara+rspec 一、有Gemfile里添加gem引用 gem 'rspec...

  • RSpec cheatsheet

    RSpec cheatsheet�RSpec 是Ruby的一个测试框架,以下是它的一些常用法cheatsheet...

  • RSpec Trial

    开篇 这篇Deck是近期对RSpec测试框架的一个Session总结,参考了RSpec官方文档和Better Rs...

  • css命名整理

    文章整理了Web前端开发中的各种CSS规范,包括文件规范、注释规范、命名规范、书写规范、测试规范等。 一、文件规范...

  • css命名规范整理

    文章整理了Web前端开发中的各种CSS规范,包括文件规范、注释规范、命名规范、书写规范、测试规范等。 一、文件规范...

  • python基础--基本数据类型和操作

    rspec是ruby的单元测试框架 ExampleGroup The object returned by des...

网友评论

    本文标题:rspec 测试书写规范

    本文链接:https://www.haomeiwen.com/subject/bgwkpxtx.html