TranspecRSpec 语法转换器

联合创作 · 2023-09-28 02:40

Transpec 是一种可通过静态和动态代码分析将你的规范转换为最新的 RSpec 语法的工具使用 Transpec,可以立即将 RSpec 2 规格升级到 RSpec 3。它支持转换几乎所有的 RSpec 的三个变化。

例子

这是一个示例规范:

describe Account do
  subject(:account) { Account.new(logger) }
  let(:logger) { mock('logger') }

  describe '#balance' do
    context 'initially' do
      it 'is zero' do
        account.balance.should == 0
      end
    end
  end

  describe '#close' do
    it 'logs an account closed message' do
      logger.should_receive(:account_closed).with(account)
      account.close
    end
  end

  describe '#renew' do
    context 'when the account is not closed' do
      before do
        account.stub(:closed?).and_return(false)
      end

      it 'does not raise error' do
        lambda { account.renew }.should_not raise_error(Account::RenewalError)
      end
    end
  end
end

Transpec 会将其转换为以下形式:

describe Account do
  subject(:account) { Account.new(logger) }
  let(:logger) { double('logger') }

  describe '#balance' do
    context 'initially' do
      it 'is zero' do
        expect(account.balance).to eq(0)
      end
    end
  end

  describe '#close' do
    it 'logs an account closed message' do
      expect(logger).to receive(:account_closed).with(account)
      account.close
    end
  end

  describe '#renew' do
    context 'when the account is not closed' do
      before do
        allow(account).to receive(:closed?).and_return(false)
      end

      it 'does not raise error' do
        expect { account.renew }.not_to raise_error
      end
    end
  end
end

安装

只需使用gem命令安装transpec

$ gem install transpec

基本用法

在转换您的规格之前:

  • 确保您的项目具有rspecgem 依赖项 2.14 或更高版本。如果没有,请更改你的Gemfile*.gemspec
  • 运行rspec并检查所有规格是否通过。
  • 确保 Git 存储库是干净的

然后,在项目根目录下运行transpec

$ cd some-project $transspec

这将运行规范,转换它们,并覆盖spec目录中的所有规范文件

转换后,rspec再次运行,检查是否一切仍然是绿色的:

$ bundle exec rspec

如果它是绿色的,请使用自动生成的消息提交更改,该消息描述转换摘要并帮助您的团队成员理解新语法:

$ git commit -aeF .git/COMMIT_EDITMSG
浏览 5
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报