使用RSpec进行存根

布鲁斯特

我试图理解为什么这些测试的结果是,第一个测试声称该方法未设置存根,但是第二个测试却是。

class Roll
  def initialize
    install if !installed?
  end
  def install; puts 'install'; end
end

describe Roll do
  before do
    class RollTestClass < Roll; end
    RollTestClass.any_instance.stub(:install)
  end

  let(:roll_class) { RollTestClass }
  let(:roll) { RollTestClass.new }

  context 'when installed is true' do
    before do
      roll_class.any_instance.stub(:installed?).and_return(true)
    end

    it 'should not call install' do
      expect(roll).to_not have_received(:install)
    end
  end

  context 'when installed is false' do
    before do
      roll_class.any_instance.stub(:installed?).and_return(false)
    end

    it 'should call install' do
      expect(roll).to have_received(:install)
    end
  end
end

错误消息也很奇怪expected to have received install,但是我认为这很可能只是RSpec DSL的错误反馈。但也许值得注意。

  1) Roll when installed is true should not call install
     Failure/Error: expect(roll).to_not have_received(:install)
       #<RollTestClass:0x10f69ef78> expected to have received install, but that method has not been stubbed.
彼得·阿尔夫文

RSpec的“间谍模式”要求对象先前已存根。但是,any_instance.stub除非/直到在特定对象上调用该方法,否则实际上并不会“真正”添加该方法。这样,方法显示为“未插入”,并且您收到了所得到的错误。这是一些演示定义更改的代码:

class Foo
end

describe "" do
  it "" do
    Foo.any_instance.stub(:bar)
    foo1 = Foo.new
    foo2 = Foo.new
    print_bars = -> (context) {puts "#{context}, foo1#bar is #{foo1.method(:bar)}, foo2#bar is #{foo2.method(:bar)}"}
    print_bars['before call']
    foo1.bar
    print_bars['after call']
  end
end

产生以下输出:

before call, foo1#bar is #<Method: Foo#bar>, foo2#bar is #<Method: Foo#bar>
after call, foo1#bar is #<Method: #<Foo:0x007fc0c3842ef8>.bar>, foo2#bar is #<Method: Foo#bar>

我在RSpec的github网站上报告了这个问题,并得到了确认/响应

可以使用以下替代方法,具体取决于最近引入的expect_any_instance_of方法。

class Roll
  def initialize
    install if !installed?
  end
  def install; puts 'install'; end
end

describe Roll do
  before do
    class RollTestClass < Roll; end
  end

  let(:roll_class) { RollTestClass }
  let(:roll) { RollTestClass.new }

  context 'when installed is true' do
    before do
      roll_class.any_instance.stub(:installed?).and_return(true)
    end

    it 'should not call install' do
      expect_any_instance_of(roll_class).to_not receive(:install)
      roll
    end
  end

  context 'when installed is false' do
    before do
      roll_class.any_instance.stub(:installed?).and_return(false)
    end

    it 'should call install' do
      expect_any_instance_of(roll_class).to receive(:install)
      roll
    end
  end
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我应该如何在全局使用RSpec存根方法?

来自分类Dev

使用SinonJS对库进行存根

来自分类Dev

如何使用Fospec :: Compute对象的Rspec any_instance存根?

来自分类Dev

Rspec存根提取模型

来自分类Dev

RSpec:如何对继承的方法current_user(不带Devise)进行存根?

来自分类Dev

在Rspec中存根ActionMailer

来自分类Dev

如何在RSpec中使用class_double对类方法进行存根?

来自分类Dev

Rspec-如何对第三方异常进行存根

来自分类Dev

在rspec中存根require语句?

来自分类Dev

Rspec如何对在方法中创建的对象进行存根

来自分类Dev

RSpec 3:如何从尚未构建/添加的代码中对方法和常量进行存根?

来自分类Dev

如何使用rspec / rspec-mocks存根类方法

来自分类Dev

存根使用Sinon进行回调的方法

来自分类Dev

如何正确使用存根在rspec中进行测试

来自分类Dev

使用secure_random在rspec中存根随机值

来自分类Dev

Rails-Rspec-存根参数

来自分类Dev

Rspec 3存根用户输入

来自分类Dev

存根或模拟实例方法rspec

来自分类Dev

使用rspec在控制器中存根API调用

来自分类Dev

Rspec-存根模块方法

来自分类Dev

我应该如何在全局使用RSpec存根方法?

来自分类Dev

您是否必须在Rspec Ruby中对方法进行存根

来自分类Dev

Rspec存根公共方法

来自分类Dev

Rspec存根提取模型

来自分类Dev

Rspec-如何对第三方异常进行存根

来自分类Dev

Rspec使用控制器存根检查标题

来自分类Dev

使用RSpec在控制器中存根特定方法

来自分类Dev

使用Sinon对猫鼬模型进行存根

来自分类Dev

RSpec 存根服务对象方法