相关疑难解决方法(0)

使用Rspec,如何在Rails 3.0.11中测试我的控制器的JSON格式?

我已经浏览了网页,但是,唉,我似乎无法让Rspec正确发送内容类型,所以我可以测试我的JSON API.我正在使用RABL gem用于模板,Rails 3.0.11和Ruby 1.9.2-p180.

我的卷曲输出,工作正常(应该是401,我知道):

mrsnuggles:tmp gaahrdner$ curl -i -H "Accept: application/json" -X POST -d @bleh http://localhost:3000/applications
HTTP/1.1 403 Forbidden 
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache
X-Ua-Compatible: IE=Edge
X-Runtime: 0.561638
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)
Date: Tue, 06 Mar 2012 01:10:51 GMT
Content-Length: 74
Connection: Keep-Alive
Set-Cookie: _session_id=8e8b73b5a6e5c95447aab13dafd59993; path=/; HttpOnly

{"status":"error","message":"You are not authorized to access this page."}
Run Code Online (Sandbox Code Playgroud)

我的一个测试用例中的示例:

describe ApplicationsController do
  render_views
  disconnect_sunspot

  let(:application) { Factory.create(:application) }

  subject { application }

  context "JSON" do

    describe "creating a new application" do

      context …
Run Code Online (Sandbox Code Playgroud)

api json rspec ruby-on-rails header

58
推荐指数
2
解决办法
6万
查看次数

为什么RSpec的方法,"获取","发布","放置","删除"不在gem(或Rails外)的控制器规范中工作?

我不是Rails或Rspec的新手,但我是制作宝石的新手.当我测试我的控制器时,REST方法"get","post","put","delete"给我一个未定义的方法错误.

您可以在下面找到代码,但如果您希望在贴图中看到它,请单击此处.

谢谢!

这是我的spec_helper:


$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems'
require 'active_support' unless defined? ActiveSupport # Need this so that mattr_accessor will work in Subscriber module
require 'active_record/acts/subscribable'
require 'active_record/acts/subscriber'
require 'action_view'
require 'action_controller' # Since we'll be testing subscriptions controller
#require 'action_controller/test_process'
require 'spec'
require 'spec/autorun'


# Need active_support to user mattr_accessor in Subscriber module, and to set the following inflection
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'dorkus', 'dorkuses'
end                     

require 'active_record' # Since we'll be testing a User …
Run Code Online (Sandbox Code Playgroud)

ruby gem rspec ruby-on-rails

20
推荐指数
3
解决办法
2万
查看次数

尝试从 Rails 控制台使用 JSON 语法测试 HTTP 请求

我正在为 API 编写一些 rspec,并尝试直接在 Rails 控制台中测试 HTTP 请求代码,以便我可以看到返回的结果。

例如,我的规范中有以下几行

      json_data = '{"user": {"email": "#{user.email}", "password": "#{user.password}"}}'
      post "api/v1/users/sign_in", json_data  
      @token = json(last_response)[:client][:auth_token]
      get 'api/v1/users', {}, {'Authorization' => @token}
Run Code Online (Sandbox Code Playgroud)

此代码不起作用,因为我的“json()”函数未正确解析响应。我想使用 JSON 参数获取这些“post”和“get”,并使用 Net::HTTP 在 Rails 控制台中测试它们,这样我就可以看到返回的响应并调试我的规范代码。有人知道该怎么做吗?

如果重要的话,我尝试使用的授权令牌是由 Devise 生成的。

json rspec ruby-on-rails devise

5
推荐指数
1
解决办法
6718
查看次数

rails升级到4.2.8后无法记录"render_template.action_view"事件

我正在升级主要服务于JSON的rails应用程序.我能够升级到的最后一个工作版本是4.1.升级到4.2后,请求在测试日志中产生奇怪错误的规范:

Could not log "render_template.action_view" event. NoMethodError: undefined method `render_views?' for #<Class:0x007fe544a2b170>
Run Code Online (Sandbox Code Playgroud)

在某处我读到这是因为rails尝试渲染不存在的视图.在跳到rails 4之前,我们设置好了headers['CONTENT_TYPE'] = 'application/json'一切.我读到这不再适用于rails 4.我已经尝试添加format: :json,如下所示:将Rspec默认GET请求格式设置为JSON,这没有帮助.

如何获得规格再次运行的任何帮助将不胜感激.

json rspec ruby-on-rails ruby-on-rails-4 ruby-on-rails-4.2

1
推荐指数
1
解决办法
686
查看次数