11

私はクラスベースのビューを持っています

class HomePage(View):
   def get(self, request):
       return HttpResponse('<p>This is content.</p>')

以下のように定義された url-pattern:

urlpatterns = patterns('',
                  url(r'^$', HomePage.as_view()),
              )

このパターンが現在のビュー関数に解決されるように、次のようなテストを作成しました。

class HomePageTest(TestCase):

def test_root_url_resolves_to_home_page_view(self):
    found = resolve('/')
    self.assertIsInstance(found.func, HomePage)

この単体テストを実行すると、次のエラーが発生します。

self.assertIsInstance(found.func, HomePage)
AssertionError: <function HomePage at 0x7f85dd2c7840> is not an instance of <class 'web.views.HomePage'>

このケースをテストする方法はありますか?

4

4 に答える 4

0

Valentjediがうまくいかなかったので、別の方法でやりました。やった 。:

class HomePageTest(TestCase):
    def test_root_url_resolves_to_home_page_view(self):
        found = resolve('/')
        self.assertEqual(found.view_name, "home")

それが役に立てば幸い

于 2018-02-24T16:31:07.860 に答える