使用 mocha 模拟 Rails 应用程序对象
我正在编写一个生成器,我需要模拟 Rails.application 对象并返回 Rails.application.class.parent 作为 Rails 应用程序的名称。
def test_model_with_application_namespace
name = "Dummyapp"
application = Rails.stubs(:application).class.parent.returns(name)
run_generator ["file", "--namespaced"]
assert_file "app/models/myapp/file.rb", /class Dummyapp::File < ActiveRecord::Base/
end
这就是我到目前为止所进行的测试。
I'm writing a generator and I need to get mock a Rails.application object and get back the Rails.application.class.parent as the name of the Rails application.
def test_model_with_application_namespace
name = "Dummyapp"
application = Rails.stubs(:application).class.parent.returns(name)
run_generator ["file", "--namespaced"]
assert_file "app/models/myapp/file.rb", /class Dummyapp::File < ActiveRecord::Base/
end
This is what I have so far for my test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
Rails.application.class
返回的对象作为响应parent
并返回name
的模拟。现在,您只需删除应用程序即可。您需要将父级、类和应用程序作为模拟。可能有一种更干净的方法,但我认为这可以满足您的要求:You need the object retured by
Rails.application.class
to be a mock that responds toparent
and returnsname
. Right now, you just stub out application. You need parent, class, and application to be mocks. There is probably a cleaner way of doing it, but I think this will do what you want: