我试着测试我正在开发的Api。我也得到了正确的回答,但是日期格式似乎已经改变了
2018-11-27 18:03:44.000000000 +0000
到
2018-11-27T18:03:44.000Z
expected: [{"created_at"=>2018-11-27 18:03:44.000000000 +0000, "email"=>"jennettesaway...UfR7oeVa5.ZaWXce2qtn6Em2oSQuH6Iljqhx61BI7cvE3CG", "updated_at"=>2018-11-27 18:03:44.000000000 +0000}]
got: [{"created_at"=>"2018-11-27T18:03:44.000Z", "email"=>"georgene@champlin.biz...4$JlzdvIUfR7oeVa5.ZaWXce2qtn6Em2oSQuH6Iljqhx61BI7cvE3CG", "updated_at"=>"2018-11-27T18:03:44.000Z"}]
RSpec.describe UsersController, type: :request do
let!(:user) { Fabricate(:user) }
let(:valid_attributes) { Fabricate.attributes_for :user }
let(:invalid_attributes) { Fabricate.attributes_for :invalid_user }
# set headers for authorization
#let(:headers) { { 'Authorization' => token_generator(user.id) } }
let(:headers) { valid_headers }
describe "GET #index" do
before(:each) do
#allow(request).to receive(:headers).and_return(headers)
end
it "returns a success response" do
get "/users", params: {}, headers: valid_headers
expect(json).to eq [user.as_json]
end
end
end
那是我的控制器
class UsersController < ApplicationController
skip_before_action :authorize_request, only: :create
before_action :set_user, only: [:show, :update, :destroy]
def index
@users = User.all
render json: @users
end
end
json
def json
JSON.parse(response.body)
end