0

我正在尝试使用培根在 ruby​​motion 中为我的控制器编写功能测试。

在我的控制器中,我有一些填充一些数据的代码:

def viewWillAppear(animated)
  song_text.text = chant.song_text
  ...
end

chant当我将该控制器推到导航控制器时,我正在设置变量。

这在应用程序中运行良好,但是当我尝试在规范的前块中执行此操作时,它不起作用,因为viewWillAppear在块之前被调用并且它以NoMethodError: undefined method 'song_text' for nil:NilClass.

有没有办法处理这种情况?是否有其他方法来填充数据或使用不同的方法viewWillAppear

4

2 回答 2

3

我遇到了类似的问题,通过在方法中调用方法super解决了viewWillAppear

于 2013-03-12T15:41:10.653 回答
0

The RubyMotion support answered my support ticket indicating, that this is the expected behavior of controllers in bacon.

I worked around the whole thing by stubbing my class under test in the bacon spec like so:

# spec/chant_controller_spec.rb
class ChantControllerStub < ChantController
  def chant
    @chant ||= create_chant
  end
end

describe "ChantController" do
  tests ChantControllerStub

  it "displays a chant" do
    view('verse').should.not == nil
  end
end
于 2013-04-05T20:32:00.217 回答