문제

어 컨트롤러 방법은 다음과 같:

def show
  @model = Model.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => model }
  end
end

최선의 방법은 무엇일을 작성하는 통합 테스트를 주장되는 이익이 예상된 XML?

도움이 되었습니까?

해결책

조합의 형식을 사용하고 assert_select 에서는 통합 시험동:

class ProductsTest < ActionController::IntegrationTest
  def test_contents_of_xml
    get '/index/1.xml'
    assert_select 'product name', /widget/
  end
end

자세한 내용을 확인 assert_select 레일에 도움을 줍니다.

다른 팁

응답에서 ntalbott 여 얻는 작업입니다.게시물 작업은 조금 더 까다;보내고 싶은 경우 새로운 개체로 XML 메시지고 있는 XML 특성에 표시 params hash 컨트롤러에서,당신은 헤더를 오른쪽.여기를 들어(레일 2.3.×):

class TruckTest < ActionController::IntegrationTest
  def test_new_truck
    paint_color = 'blue'
    fuzzy_dice_count = 2
    truck = Truck.new({:paint_color => paint_color, :fuzzy_dice_count => fuzzy_dice_count})
    @headers ||= {}
    @headers['HTTP_ACCEPT'] = @headers['CONTENT_TYPE'] = 'application/xml'
    post '/trucks.xml', truck.to_xml, @headers
    #puts @response.body
    assert_select 'truck>paint_color', paint_color
    assert_select 'truck>fuzzy_dice_count', fuzzy_dice_count.to_s
  end
end

당신은 여기에서 볼 수 있는 2 인수하는 게시물을 수 없는 매개변수 해시;할 수 있는 문자열(을 포함하는 XML), 는 경우 헤더는 옳습니다.3 인수,@헤더 부분입니다 걸렸다 많은 연구원합니다.

(참고도 사용 to_s 비교할 때 정수 값에 assert_select.)

이것은 관용적 방법의 테스트는 xml 응답에서 컨트롤러입니다.

class ProductsControllerTest < ActionController::TestCase
  def test_should_get_index_formatted_for_xml
    @request.env['HTTP_ACCEPT'] = 'application/xml'
    get :index
    assert_response :success
  end
end

이러한 2 개의 답변은 좋은 것을 제외하고,내 결과를 포함 datetime 필드에 있는 공 다른 대부분의 상황에서,그래서 assert_equal 실패합니다.그것은 나타나는 것이 필요하는 프로세스 포함 @response.body 를 사용하여 XML 파서,그리고 다음을 비교하는 개인 분야,수소,등등.또는 쉬운 방법이 있을까?

설정 요청을 개체 accept header:

@request.accept = 'text/xml' # or 'application/xml' I forget which

할 수 있습니다 다음 주장 응답 본문과 동일하여 무엇을 기대하고 계신

assert_equal '<some>xml</some>', @response.body
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top