如何在Rails中实现request.subdomains的多种模拟与存根技巧?
- 内容介绍
- 文章标签
- 相关推荐
本文共计124个文字,预计阅读时间需要1分钟。
我在我的Rails应用程序中编写了一些功能测试,在`application_controller.rb`中我使用了以下代码:
rubybefore_filter :current_accountdef current_account @current_account=current_account || Account.find_by_subdomain!(request.subdomains.first)end
before_filter :current_account def current_account @current_account ||= Account.find_by_subdomain!(request.subdomians.first) end
运行测试时,request.subdomains不包含我正在寻找的有效子域,因此无法运行任何功能测试.
是否可以存根current_account方法或模拟request.subdomains对象?
在你的功能测试中你应该能够做到(使用mocha):@request.expects(:subdomains).returns(['www'])
本文共计124个文字,预计阅读时间需要1分钟。
我在我的Rails应用程序中编写了一些功能测试,在`application_controller.rb`中我使用了以下代码:
rubybefore_filter :current_accountdef current_account @current_account=current_account || Account.find_by_subdomain!(request.subdomains.first)end
before_filter :current_account def current_account @current_account ||= Account.find_by_subdomain!(request.subdomians.first) end
运行测试时,request.subdomains不包含我正在寻找的有效子域,因此无法运行任何功能测试.
是否可以存根current_account方法或模拟request.subdomains对象?
在你的功能测试中你应该能够做到(使用mocha):@request.expects(:subdomains).returns(['www'])

