2013-07-10 2 views
0

테스트 할 특정 노드를 격리하고 싶습니다.특정 노드를 응답으로 테스트하는 Rspec

대신

get :show 
response.should have_content(@user.name) 

의 그것은 올바른/더 자세한 설명은

get :show 
profile = response.find_selector("div.user-profile") 
profile.should have_content(@user.name) 

같은 것이 가능하다을 쓸 수 있을까?

UPDATE 베드로의 대답을 읽는하지만 여전히 요소를 찾지 못하는 한 후이와 조금 더있어. spec\controllers\users_controller_spec.rb 첫 번째 테스트가 통과

require 'spec_helper' 

describe UsersController do 

    render_views 

    it "should should have header" do 
    get :index 
    response.should have_selector("h1", content: "Users")   
    end 

    it "should show user profile" do 
    get :index 
    node = page.find_by_id("test") 
    p node 
    end 

end 

app\views\users\index.html.erb

<h1>Users</h1> 
<div id="test"></div> 

는 제 2 테스트 ElementNotFound 오류 준다. 이것이 Rails에서 처음 시도한 바보 일뿐입니다.

답변

1

예, 가능합니다. 카피 바라에는 find_selector이 없지만 find 및 파생 상품은 locator이며 위의 암시대로 작동합니다. 당신은 말할 수

page.should have_selector('foo', text: 'bar') 

: 대신에, 예를 들어 http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders

참조

node = page.find('foo') 
node.should have_content('bar') 
+0

안녕 피터, 나는이 작업을 얻을 수 없다, 당신은 예를 줄 수주십시오? 'get : index; Capybara :: Node :: Simple.new (response.body) .find ("# test");'이 요소를 찾지 못했습니다. 나는 Ruby에 익숙하지 않기 때문에 아마도 문서를 잘못 읽었을 것이다. – fearofawhackplanet

+0

기존 테스트 중 하나를 사용하여 방금 테스트 한 예제에 대한 업데이트 된 답변을 참조하십시오. 코드 스 니펫이 작동하지 않는 이유에 대해서는 더 많이 볼 필요가 있습니다. 질문에 제시 한 예를 수정 해 보았습니까? –

+0

업데이트 된 질문이 샘플 코드 – fearofawhackplanet

관련 문제