# Testing UpdateView

**URL:** <https://forum.djangoproject.com/t/testing-updateview/22221>\
**Category:** Forms & APIs\
**Created:** [July 11, 2023, 3:18pm UTC](https://forum.djangoproject.com/t/testing-updateview/22221 "2023-07-11T15:18:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcd1000](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pcd1000/32/4499_2.png) [@pcd1000](https://forum.djangoproject.com/u/pcd1000)\
**Post date:** [July 11, 2023, 3:18pm UTC](https://forum.djangoproject.com/t/testing-updateview/22221/1 "2023-07-11T15:18:23Z")

</div>

I need some help testing a class based view

I have a view (UpdateView) that after saving any updates, checks if a field, ‘date\_time’, is present in form.changed\_data and if it’s not present then changes are made to another model.

This works perfectly in practice, but I am finding it difficult to test, because whenever I post data using;

```auto
response = self.client.post(reverse('web-edit'), data=update_data)

```

All of the fields are marked as having changed in form.changed\_data including the ones that are the same.

**View**

```auto
class WebUpdateView(UpdateView):
    model = Web
    form_class = WebAddForm
    template_name = 'web/webinar-add-update.html'

    def form_valid(self, form):
        self.object = form.save()

        if not 'date_time' in form.changed_data:
            do something to another model

```

Test

```auto
class WebinarUpdateTest(TestCase):

    def setUp(self):
        self.now = datetime.now(tz=pytz.UTC)

        self.web = Web.objects.create(title='test title',
                                      description='Test description',
                                      date_time=self.now
                                      )

    def test_post_webinar_add_two(self):
        update_data = {'title': 'test title 2',
                       'description': 'Test description 2',
                       'date_time': self.now}

        response = self.client.post(reverse('web-edit'), data=update_data)

```

With the above code, all fields are marked as changed, when clearly the date\_time field hasn’t. So my question is, how can I test the ‘if not ‘date\_time’ in form.changed\_data:’ statement in my view?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 11, 2023, 4:21pm UTC](https://forum.djangoproject.com/t/testing-updateview/22221/2 "2023-07-11T16:21:23Z")

</div>

Not particularly familiar with this, but just as a guess / diagnostic item, what happens if you make that test _before_ you save the object?

---

<div class="post-metadata">

**Author:** ![pcd1000](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pcd1000/32/4499_2.png) [@pcd1000](https://forum.djangoproject.com/u/pcd1000)\
**Post date:** [July 11, 2023, 5:14pm UTC](https://forum.djangoproject.com/t/testing-updateview/22221/3 "2023-07-11T17:14:22Z")

</div>

Hi ken,

Thank you for your time.

This is what I came up with, although it feels like a bit of a ‘hack’:

```auto
    def test_post_webinar_with_date_time(self):
        update_data = {'title': 'test title 2',
                       'description': 'Test description 2',
                       'date_time': self.now}

        form = WebAddForm(data=update_data, instance=self.web)
        form.is_valid()
        dt = form.changed_data.index('date_time')
        form.changed_data.pop(dt)
        view = WebUpdateView()
        view.object = self.web
        view.request = RequestFactory().post('/')
        view.form_valid(form)

```
