小编Rob*_*345的帖子

在RSpec中记录RestClient响应

我有以下规格......

  describe "successful POST on /user/create" do
    it "should redirect to dashboard" do
      post '/user/create', {
          :name => "dave",
          :email => "dave@dave.com",
          :password => "another_pass"
      }
      last_response.should be_redirect
      follow_redirect!
      last_request.url.should == 'http://example.org/dave/dashboard'
    end
  end
Run Code Online (Sandbox Code Playgroud)

Sinatra应用程序上的post方法使用rest-client调用外部服务.我需要以某种方式存根其余的客户端调用以发回预设的响应,因此我不必调用实际的HTTP调用.

我的申请代码是......

  post '/user/create' do
    user_name = params[:name]
    response = RestClient.post('http://localhost:1885/api/users/', params.to_json, :content_type => :json, :accept => :json)
    if response.code == 200
      redirect to "/#{user_name}/dashboard"
    else
      raise response.to_s
    end
  end
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何用RSpec做到这一点吗?我已经用Google搜索并发现了许多博客文章,这些文章从表面上划过,但我实际上找不到答案.我对RSpec时期很新.

谢谢

ruby rspec sinatra rest-client

11
推荐指数
2
解决办法
1万
查看次数

对象上的 Ruby to_json 输出实例变量

我正在使用 Sinatra 并尝试通过使用 'json' gem 并调用 .to_json 方法来输出 JSON 中的对象。我期望输出为 JSON,其中包含 attr_reader 部分中的符号及其值。

这是我的代码。我需要做一些特别的事情才能让它发挥作用吗?

require "sinatra"
require "json"    

class Foo
  attr_reader :id, :name

  def initialize(id, name)
    @id = id
    @name = name
  end
end

get '/start' do
  content_type :json
  Foo.new(2, "john").to_json
end
Run Code Online (Sandbox Code Playgroud)

我从输出中得到的只是对象默认的 to_s。

"#<Foo:0x007fe372a3ba80>"
Run Code Online (Sandbox Code Playgroud)

ruby json sinatra

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

标签 统计

ruby ×2

sinatra ×2

json ×1

rest-client ×1

rspec ×1