나는 내 컨트롤러거 다음 코드:
format.json { render :json => {
:flashcard => @flashcard,
:lesson => @lesson,
:success => true
}
I want to enable-volume-set 스펙 컨트롤러거 내 특정 책정안 높여줍니까 테스트 성공 json 응답을 받을 그래서 다음 행:
controller.should_receive(:render).with(hash_including(:success => true))
비록 내 달릴 땐 저도 테스트 중 오류:
Failure/Error: controller.should_receive(:render).with(hash_including(:success => false))
(#<AnnoController:0x00000002de0560>).render(hash_including(:success=>false))
expected: 1 time
received: 0 times
내가 검사 응답 잘못?
이 책에는 예상값 확인한 응답 객체에는 검사할 수 있습니다.
@expected = {
:flashcard => @flashcard,
:lesson => @lesson,
:success => true
}.to_json
get :action # replace with action name / params as necessary
response.body.should == @expected
이 때문에 난이도 게시물로의 '조금' 로 변경. # 39 의 here& 처리할 수 있는 방법이 없다.
it "responds with JSON" do
my_model = stub_model(MyModel,:save=>true)
MyModel.stub(:new).with({'these' => 'params'}) { my_model }
post :create, :my_model => {'these' => 'params'}, :format => :json
response.body.should == my_model.to_json
end
참고로 'mock_model' 은 '또는' 진짜 '때문에' to_json 응답하지 인컨텍스트 stub_model 모델 인스턴스 필요하다.
# 39 의 케빈 Trowbridge& 오토메이티드 의 구축되므로 오프하도록
response.header['Content-Type'].should include 'application/json'
쉽고 간단한 방법이 있을거야.
# set some variable on success like :success => true in your controller
controller.rb
render :json => {:success => true, :data => data} # on success
spec_controller.rb
parse_json = JSON(response.body)
parse_json["success"].should == true
Spec / 지원 / '도우미' 인사이드라면 함수를 정의할 수도 있습니다
module ApiHelpers
def json_body
JSON.parse(response.body)
end
end
RSpec.configure do |config|
config.include ApiHelpers, type: :request
end
및 사용 할 때마다 '' json_body JSON 액세스하려면 반응을 보였다.
예를 들어, δ1 요청이 사양명세 바로 사용할 수 있습니다.
context 'when the request contains an authentication header' do
it 'should return the user info' do
user = create(:user)
get URL, headers: authenticated_header(user)
expect(response).to have_http_status(:ok)
expect(response.content_type).to eq('application/vnd.api+json')
expect(json_body["data"]["attributes"]["email"]).to eq(user.email)
expect(json_body["data"]["attributes"]["name"]).to eq(user.name)
end
end
response.header['Content-Type'].should include 'text/javascript'
내가 찾은 고객 정규 있습니다. https://raw.github.com/gist/917903/92d7101f643e07896659f84609c117c4c279dfad/have_content_type.rb
및 smartupdate spec/support/matchers/have_content_type.rb 로드하기에 물건을 넣을 수 spec/spec_helper.rb 충스러웠으니 지원을 통해 이 같은 일이
Dir[Rails.root.join('spec/support/**/*.rb')].each {|f| require f}
다음은 코드 자체가 사라져 버렸다고 밝히고, 만일의 사태에 대비해 지정된 링크.
RSpec::Matchers.define :have_content_type do |content_type|
CONTENT_HEADER_MATCHER = /^(.*?)(?:; charset=(.*))?$/
chain :with_charset do |charset|
@charset = charset
end
match do |response|
_, content, charset = *content_type_header.match(CONTENT_HEADER_MATCHER).to_a
if @charset
@charset == charset && content == content_type
else
content == content_type
end
end
failure_message_for_should do |response|
if @charset
"Content type #{content_type_header.inspect} should match #{content_type.inspect} with charset #{@charset}"
else
"Content type #{content_type_header.inspect} should match #{content_type.inspect}"
end
end
failure_message_for_should_not do |model|
if @charset
"Content type #{content_type_header.inspect} should not match #{content_type.inspect} with charset #{@charset}"
else
"Content type #{content_type_header.inspect} should not match #{content_type.inspect}"
end
end
def content_type_header
response.headers['Content-Type']
end
end