为 facebooker 控制器编写功能测试?

发布于 2024-08-20 07:13:47 字数 862 浏览 4 评论 0原文

有人对在功能测试中模拟 Facebook 请求的最佳实践有什么建议吗?是否就像将所有正确的参数添加到请求中一样简单?有办法把它们去掉吗?

我正在使用 facebooker,它带有模拟服务:

  # A mock service that reads the Facebook response from fixtures
  # Adapted from http://gist.github.com/44344
  #
  #   Facebooker::MockService.fixture_path = 'path/to/dir'
  #   Facebooker::Session.current = Facebooker::MockSession.create

但是当我编写基本的 get 测试时,它会尝试将浏览器重定向到 facebook 页面以添加应用程序,我认为这表明模拟不起作用。

  test "loads respondent" do
    Facebooker::Session.current = Facebooker::MockSession.create
    get :index
    puts @response.body # => <html><body>You are being <a href="http://www.facebook.com/install.php?api_key=65e9d2c74b295cc5bcea935b584557f6&amp;v=1.0&amp;next=http%3A%2F%2Ftest.host%2Ffacebook">redirected</a>.</body></html>
  end

Anyone have any tips for best practices for mocking out facebook requests in functional tests? Is it just as simple as adding all of the proper params to the request? Is there a way to stub those out?

I'm using facebooker, which comes with a mock service:

  # A mock service that reads the Facebook response from fixtures
  # Adapted from http://gist.github.com/44344
  #
  #   Facebooker::MockService.fixture_path = 'path/to/dir'
  #   Facebooker::Session.current = Facebooker::MockSession.create

But when I write a basic get test, it tries to redirect the browser to the facebook page for adding the app, which I assume indicates that the mocking isn't working.

  test "loads respondent" do
    Facebooker::Session.current = Facebooker::MockSession.create
    get :index
    puts @response.body # => <html><body>You are being <a href="http://www.facebook.com/install.php?api_key=65e9d2c74b295cc5bcea935b584557f6&v=1.0&next=http%3A%2F%2Ftest.host%2Ffacebook">redirected</a>.</body></html>
  end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-08-27 07:13:47

我在最新版本的 facebooker (1.0.58) 上得到了这个工作:

# test_helper.rb
require 'facebooker/mock/session'
require 'facebooker/mock/service'

Facebooker::MockService.fixture_path = File.join(RAILS_ROOT, 'test', 'fixtures', 'facebook')

显然你必须在固定装置中创建 facebook 目录,或者把它放在任何地方。在里面,您必须为每个 facebook 方法添加一个文件夹,并为您想要测试的不同类型的响应添加一个 xml 文件。我必须添加 facebook.users.getInfo 和 facebook.users.hasAppPermission 。最简单的方法就是添加一个名为 default.xml 的文件,其中包含来自 facebook wiki 的示例代码以执行这些操作。

 # Controller test
 test "facebook action" do  
   get :index, {:fb_sig_added => true}, :facebook_session => Facebooker::MockSession.create
   assert_response :success
 end

据我所知,fb_sig_added 参数是必要的,因为内部 facebooker 逻辑在检查该参数的会话之前直接检查参数。这对我来说似乎有点古怪,但也许是有原因的。

I got this working with the latest version of facebooker (1.0.58):

# test_helper.rb
require 'facebooker/mock/session'
require 'facebooker/mock/service'

Facebooker::MockService.fixture_path = File.join(RAILS_ROOT, 'test', 'fixtures', 'facebook')

Obviously you will have to create the facebook directory in fixtures, or put it wherever. Inside you have to add a folder for each facebook method, and an xml file for the different types of responses you want to test for. I had to add facebook.users.getInfo and facebook.users.hasAppPermission. The easiest is just to add a file named default.xml with the example code from the facebook wiki for those actions.

 # Controller test
 test "facebook action" do  
   get :index, {:fb_sig_added => true}, :facebook_session => Facebooker::MockSession.create
   assert_response :success
 end

The fb_sig_added param is necessary as far as I can tell, because the internal facebooker logic checks the params directly before checking the session on that one. Which seems a bit wanky to me but maybe there's a reason for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文