【问题标题】:Mocking json response in ruby在 ruby​​ 中模拟 json 响应
【发布时间】:2014-08-14 08:37:58
【问题描述】:

我从我的 API 中获得了 json response。有没有办法我们可以模拟这个 json 响应以将其与 API 进行比较。

it "should find all accounts" do 
  let(:myjson) {
            "@type": "userResource",
            "createdAt": "2014-08-14T07:32:52",
            "createdBy": 2,
            "updatedAt": "2014-08-14T07:32:52",
            "updatedBy": 2,
            "email": "12@12.com",
            "name": "test",
            "password": "",
            "confirmpassword": "",
            "id": 2856,
            "accountid": 0,
            "resetpw": false,                
   }
  expect(response.body).to eq(myjson);
emd

有没有一种方法可以模拟 json,这样我就不需要在我的规范中对其进行硬编码。

【问题讨论】:

    标签: ruby-on-rails ruby json unit-testing rspec


    【解决方案1】:

    我猜您在进行 API 调用之前已经进行了某种设置以在数据库中创建帐户,因此您可以在测试中将这些帐户序列化为 JSON,并将其与 API 响应进行比较。

    类似这样的:

    accounts = [ Account.create(...), Account.create(...) ]
    expected_response = accounts.as_json
    expect(response.body).to eq(expected_response)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 2017-04-27
      • 2015-05-03
      相关资源
      最近更新 更多