加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何修复2.99版本的RSpec语法?

发布时间:2020-12-17 03:49:10 所属栏目:百科 来源:网络整理
导读:我最近将我的Rails 4应用程序从RSpec 2.X升级到2.99,尽管已经运行了 Transpec,但我的一些测试仍然失败. require 'spec_helper'describe Invoice,:type = :model do before :each do @user = FactoryGirl.create(:user) @invoice = FactoryGirl.create(:invoi
我最近将我的Rails 4应用程序从RSpec 2.X升级到2.99,尽管已经运行了 Transpec,但我的一些测试仍然失败.

require 'spec_helper'

describe Invoice,:type => :model do

  before :each do
    @user = FactoryGirl.create(:user)
    @invoice = FactoryGirl.create(:invoice,:user => @user)
  end

  it "is not open" do
    expect {
      FactoryGirl.create(:payment,:invoice => @invoice,:amount => 100)  
    }.to change{@invoice.reload.open?}.from(true).to(false)
  end

  it "is open" do
    expect {
      FactoryGirl.create(:payment,:amount => 99.99)  
    }.to_not change{@invoice.reload.open?}.to(false)
  end

end

第一次测试就像RSpec升级之前一样.

但是,第二个测试会抛出错误:

Failure/Error: expect {
   `expect { }.not_to change { }.to()` is deprecated.

我必须将语法更改为什么?

我已经尝试了一些像not_to,be_falsey等的东西.到目前为止,没有任何工作.

谢谢你的帮助.

解决方法

不要断言值不会改变为某些东西,只是断言它不会改变:

it "is open" do
  expect {
    FactoryGirl.create(:payment,:amount => 99.99)  
  }.to_not change { @invoice.reload.open? }
end

那不会测试@ invoice.reload.open?的初始值,但是你应该对它进行单独的测试.您不需要在此测试中再次测试它.

尽管如此,在RSpec 3中,您可以单独使用.from来测试未更改的值是否具有给定的初始值:

it "is open" do
  expect {
    FactoryGirl.create(:payment,:amount => 99.99)  
  }.to_not change { @invoice.reload.open? }.from(false)
end

在RSpec 2中你不能这样做; .to_not change {}.from如果传递给.from的值不是预期的值.在RSpec 2.99中引发警告.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读