Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Minitest模拟Sinatra应用程序_Ruby_Mocking_Sinatra_Minitest - Fatal编程技术网

Ruby Minitest模拟Sinatra应用程序

Ruby Minitest模拟Sinatra应用程序,ruby,mocking,sinatra,minitest,Ruby,Mocking,Sinatra,Minitest,我从Sinatra::Base类继承了Sinatra应用程序。在这个应用程序中,我有一个助手方法,我在before filter中使用它。如何使用minitest模拟库在测试中模拟这种方法 before do unless valid_signature? halt 401 end end 为了使有效的\u签名?成为Sinatra助手方法,它需要是模块的一部分。因此,假设您的模块是MyModule module MyModule def valid_signature?

我从
Sinatra::Base
类继承了Sinatra应用程序。在这个应用程序中,我有一个助手方法,我在before filter中使用它。如何使用minitest模拟库在测试中模拟这种方法

before do
  unless valid_signature?
    halt 401
  end
end

为了使
有效的\u签名?
成为Sinatra助手方法,它需要是模块的一部分。因此,假设您的模块是
MyModule

module MyModule
  def valid_signature?
  end
end
我们可以使用minitest模拟
有效的\u签名?
,如下所示:

MyModule.stub :valid_signature?, "stub return value" do
  # method is stubbed only in this block, so run tests from here
  # make sure your module was defined before you stub it.
end
如果从块中运行测试的限制太大,我建议您自己在测试文件中查看或在运行时手动重新定义该方法:

MyModule
  def valid_signature?
    # you can only redefine after class has already been defined.
    "stub return value"
  end
end

假设您将函数定义为实例函数或helpers块(实际上是相同的),您可以使用minitest模拟它们,如下所示:

def app
    MyApp.new
end

def test_valid_signature
    app.helpers.stub(:valid_signature?, @mock) do
        return true
    end
    # do something...
    assert valid_signature?
end

遗憾的是,它没有起作用。我得到“未定义的方法`valid_signature?for MyModule”错误。切换到摩卡不是一个选项,我认为-这个项目已经有相当多的测试与minitest使用。你确定你取代了你的实际模块名MyModule?您还需要首先确保您的模块是必需的。