2017-12-25 15 views
1

나는 다음과 같은 검사를 : 귀하의 경우에 django 2 documentation on testing the response resolver해결 된 URL 함수가 클래스 기반 뷰`as_view()`메소드와 같지 않습니까?

# class-based views need to be compared by name, as the functions 
# generated by as_view() won't be equal 
self.assertEqual(response.resolver_match.func.__name__, MyView.as_view().__name__) 

당으로

AssertionError: <function HomePageView at 0x107d65620> != <function HomePageView at 0x107d97400> 
+1

이는이다

def test_root_url_resolves_to_home_page_view(self): found = resolve('/') self.assertEqual( found.func, views.HomePageView.as_view() ) 

이 오류를 제공합니다 영형 테스트 할 일. 'as_view'는 매번 새로운 호출 가능한 객체를 반환하고, 다른 지시 객체 없이는 id로 객체를 비교합니다. 이 테스트의 요점은 무엇입니까? –

+0

이것은 'urlpatterns'를 테스트하는 방법이며 정확한'view'를 사용합니다. 여기에 함수 기반보기 예제가 사용되었습니다. https://www.obeythetestinggoat.com/book/chapter_unit_test_first_view.html – surfer190

+0

@ surfer190 공유 한 페이지 하단의 댓글을 읽으면 이것이 표시됩니다. 이미 답변되었습니다 http://www.obeythetestinggoat.com/book/chapter_unit_test_first_view.html#comment-3279904435 –

답변

0

:

self.assertEqual(
     found.func.__name__, 
     views.HomePageView.as_view().__name__ 
    )