Giter Site home page Giter Site logo

Questions about django-testing-cookbook HOT 3 CLOSED

mbrochh avatar mbrochh commented on July 21, 2024
Questions

from django-testing-cookbook.

Comments (3)

mbrochh avatar mbrochh commented on July 21, 2024

@guinslym sorry for the late answer, didn't see the email notifications and I was sick for the past four weeks or so.

  1. first, we need to create an instance of the view with views.PostUpdateView.as_view() - this call to as_view() will basically turn the class based view into a function. Then we need to actually call that function in order to execute the code in our view, so we do that with (req, pk=post.pk) - these are the arguments, that would be passed into the dispatch function of your class based view.
  2. No. Never use self.client.get() because that's slow as hell. I never ever use it. We don't test templates. Ideally, our whole frontend is rendered by ReactJS, which has it's own powerful testing capabilities with something like Jest, for example, so we aren't even using Django's templating language (which is also slow as hell and not suitable for large scale projects which might have hundreds of small partial template components).
  3. We can't write an assertion for exceptions. When the exception is thrown, it bubbles up all the way right into the test and then the test crashes with that exception. So if we want to test for 404, we need to use the context processor (with pytest.raises(Http404):). I haven't found a good way to also provide a nice test failure message. I usually just put a code-comment into the context processor code-block, so when the test crashes, the developer will open the test file and then see the comment.
  4. The point of this test was to check that one user cannot edit the object of another user. So we created the first user, and we created a Post. The Post will automatically create it's own user, so we will have two users in the system. Then we attached the first user to the request and tried to call the view, but for the object that belongs to the second user. This should raise a 404.

from django-testing-cookbook.

guinslym avatar guinslym commented on July 21, 2024

OMG!!! sorry I didn't see your reply. I am not using Github intensively now as I used to in the summer. Thanks for the reply. I will put your explication into practice.

from django-testing-cookbook.

mbrochh avatar mbrochh commented on July 21, 2024

@guinslym don't worry. Github gets overwhelming with messages quickly, so most people reply with huge delay (especially myself). It's normal :)

from django-testing-cookbook.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.