【发布时间】:2018-10-10 11:49:38
【问题描述】:
我正在使用 Rails 开发一个内部 API,我正在考虑使用 ActiceResource 来访问它。我想使用 ActiveResource 制作集成测试用例(即向测试环境中的控制器发出 http 请求),但不知道如何设置测试用例。
在正常的集成测试用例中,您使用 get/post 方法向您自己的应用发送请求。不知何故,我应该告诉 ActiceResource 连接到我的应用程序,而不是建立真正的 http 连接。
我看到的所有 ActiveResource 测试示例都使用模拟。我想避免这种情况,而是针对我的控制器代码运行我的测试。我正在使用 Rails 5 和 Minitest。
下面是显然不起作用的测试代码。如果我尝试运行它,它会报错:
require 'test_helper'
module MyApp
class User < ActiveResource::Base
self.site = 'http://www.example.com'
end
end
class UserFlowsTest < ActionDispatch::IntegrationTest
test "should get index" do
users = MyApp::User.all
assert_response :success
end
end
▶ rails test test/integration/user_flows_test.rb
NoMethodError: undefined method `response_code' for nil:NilClass
actionpack (5.2.1) lib/action_dispatch/testing/assertions/response.rb:81:in `generate_response_message'
actionpack (5.2.1) lib/action_dispatch/testing/assertions/response.rb:31:in `assert_response'
test/integration/user_flows_test.rb:13:in `block in <class:UserFlowsTest>'
【问题讨论】:
标签: ruby-on-rails activeresource