【发布时间】:2016-05-13 18:27:33
【问题描述】:
我有一个非常简单的控制器,看起来像这样。
module Veterinarians
module Dictionaries
class SpecialitiesController < ApplicationController
respond_to :json
skip_before_action :check_current_vet, only: %i( index )
def index
@specialities = Veterinarians::Speciality.all
respond_with(@specialities)
end
end
end
end
我有一个看起来像这样的 rspec 控制器测试。
require 'rails_helper'
Rails.describe Veterinarians::Dictionaries::SpecialitiesController, type: :controller do
# Not returning a response body in JSON when testing RSPEC (https://github.com/rails/jbuilder/issues/32)
render_views true
routes { Veterinarians::Engine.routes }
let(:user) { double :user, id: 123 }
before { sign_in(user) }
context '#index' do
let(:speciality) { double :speciality, id: :some_id, value: :some_val }
before { allow(Veterinarians::Speciality).to receive(:all).and_return [speciality] }
subject { get :index, format: :json }
it { is_expected.to have_http_status(:ok) }
it { expect(JSON.parse(subject.body)).to include('id' => 'some_id', 'value' => 'some_val') }
end
end
第二个示例因此错误而失败。
expected [{"__expired" => false, "name" => "speciality"}] to include {"id" => "some_id", "value" => "some_val"}
关于为什么这会失败以及带有“__expired”的哈希来自哪里的任何提示?
我还有其他使用相同测试方法成功的测试。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 rspec