首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用块会导致Rspec测试失败。

使用块会导致Rspec测试失败。
EN

Stack Overflow用户
提问于 2017-03-20 21:49:30
回答 1查看 46关注 0票数 0

我在使用Rspec中的前置块时遇到了一些问题。在下面的代码中,我无法删除allow(plane).to receive(:land_plane)行。在第二个代码块中,我也遇到了同样的问题,但是使用了allow(plane).to receive(:take_off_plane)。这里的任何帮助都将不胜感激!

代码语言:javascript
复制
describe '#land' do
    context 'when not stormy' do
      before :each do
        allow(airport.weather).to receive(:stormy_weather?).and_return(false)
        allow(plane).to receive(:land_plane)
      end
      it 'should tell the plane to land' do
        plane = double(:plane, airborne: true)
        allow(plane).to receive(:land_plane) # can't put this into before block?
        expect(airport).to receive(:land)
        airport.land(plane)
      end

      it 'should have plane in the Airport' do
        plane = double(:plane, airborne: true)
        allow(plane).to receive(:land_plane)
        airport.land(plane)
        expect(airport.planes_in_airport).to include plane
      end

      it 'should raise error when trying to land a landed plane' do
        plane = double(:plane, airborne: false)
        message = "This plane is already landed."
        expect{ airport.land(plane) }.to raise_error message
      end

      context 'when full' do
        it 'should raise an error' do
          plane = double(:plane, airborne: true)
          allow(plane).to receive(:land_plane)
          airport.capacity.times {airport.land(plane)}
          message = "Sorry. Airport full. Go away."
          expect{airport.land(plane)}.to raise_error message
        end
      end
    end

--

代码语言:javascript
复制
describe '#take_off' do
    context 'when not stormy'
    before do
      allow(airport.weather).to receive(:stormy_weather?).and_return(false)
      allow(plane).to receive(:take_off_plane)
    end
    it 'should remove plane from Airport' do
      plane = double(:plane, airborne: false)
      allow(plane).to receive(:take_off_plane) # can't delete this
      airport.planes_in_airport.push(plane)
      airport.take_off(plane)
      expect(airport.planes_in_airport).not_to include(plane)
    end
  end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-21 13:56:03

我已经想出了答案。

这里的挑战是,我错误地在预期的范围内创造了新的双打。例如:

代码语言:javascript
复制
 it 'should have plane in the Airport' do
        plane = double(:plane, airborne: true)
        allow(plane).to receive(:land_plane)
        airport.land(plane)
        expect(airport.planes_in_airport).to include plane
      end

在这里,我不应该使用plane = double(:plane, airborne: true)行。或者,在这个问题上,也有那个allow(plane).to receive(:land_plane)在里面。我应该把这两个都放在前面的街区

我的前街区应该是这样的:

代码语言:javascript
复制
before :each do
      allow(plane).to receive(:airborne).and_return(true)
      allow(plane).to receive(:land_plane)
      allow(airport.weather).to receive(:stormy_weather?).and_return(false)
    end

这就不需要我在每一个期望中创建那些奇怪的双倍了。这也意味着飞机的空降状态是预定义的.这发生在我允许平面双机接收land_plane方法之前。在此之前,我这样做是错误的,这意味着我在告诉它接受land_plane方法之后创建了一个double,从而导致了一个意外的消息异常。

谢谢你看它,如果你还有其他的观点,请告诉我!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42914580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档