Django测试没有实现对象

时间:2017-01-26 15:58:11

标签: django django-testing django-tests

我有一个django休息框架测试,它只是常规django测试的包装器,它的工作方式完全相同。代码如下所示:

lefteris@lefteris-ubuntu:~/Documents/C++/Numerical$ gcc sample.cpp -lmgl-qt -lmgl
In file included from /usr/include/mgl2/abstract.h:27:0,
                 from /usr/include/mgl2/qt.h:22,
                 from sample.cpp:1:
/usr/include/mgl2/type.h:42:20: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglPoint(mglPoint &&d):x(d.x),y(d.y),z(d.z),c(d.c) {}
                    ^
/usr/include/mgl2/type.h:42:23: error: invalid constructor; you probably meant ‘mglPoint (const mglPoint&)’
  mglPoint(mglPoint &&d):x(d.x),y(d.y),z(d.z),c(d.c) {}
                       ^
/usr/include/mgl2/type.h:117:20: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglColor(mglColor &&d):r(d.r),g(d.g),b(d.b),a(d.a) {}
                    ^
/usr/include/mgl2/type.h:117:23: error: invalid constructor; you probably meant ‘mglColor (const mglColor&)’
  mglColor(mglColor &&d):r(d.r),g(d.g),b(d.b),a(d.a) {}
                       ^
In file included from /usr/include/mgl2/mgl_cf.h:24:0,
                 from /usr/include/mgl2/mgl.h:23,
                 from /usr/include/mgl2/wnd.h:23,
                 from /usr/include/mgl2/qt.h:36,
                 from sample.cpp:1:
/usr/include/mgl2/data.h:49:18: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglData(mglData &&d):nx(d.nx),ny(d.ny),nz(d.nz),a(d.a),id(d.id),link(d.link)
                  ^
/usr/include/mgl2/data.h:49:21: error: invalid constructor; you probably meant ‘mglData (const mglData&)’
  mglData(mglData &&d):nx(d.nx),ny(d.ny),nz(d.nz),a(d.a),id(d.id),link(d.link)
                     ^
/usr/include/mgl2/data.h:570:18: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglExpr(mglExpr &&d):ex(d.ex) { d.ex=0; }
                  ^
/usr/include/mgl2/data.h:570:21: error: invalid constructor; you probably meant ‘mglExpr (const mglExpr&)’
  mglExpr(mglExpr &&d):ex(d.ex) { d.ex=0; }
                     ^
/usr/include/mgl2/data.h:601:20: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglDataV(mglDataV &&d):nx(d.nx),ny(d.ny),nz(d.nz),di(d.di),dj(d.dj),dk(d.dk),a0(d.a0)
                    ^
/usr/include/mgl2/data.h:601:23: error: invalid constructor; you probably meant ‘mglDataV (const mglDataV&)’
  mglDataV(mglDataV &&d):nx(d.nx),ny(d.ny),nz(d.nz),di(d.di),dj(d.dj),dk(d.dk),a0(d.a0)
                       ^
/usr/include/mgl2/data.h:667:20: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglDataW(mglDataW &&d):nx(d.nx),ny(d.ny),nz(d.nz),di(d.di),dj(d.dj),dk(d.dk)
                    ^
/usr/include/mgl2/data.h:667:23: error: invalid constructor; you probably meant ‘mglDataW (const mglDataW&)’
  mglDataW(mglDataW &&d):nx(d.nx),ny(d.ny),nz(d.nz),di(d.di),dj(d.dj),dk(d.dk)
                       ^
/usr/include/mgl2/data.h:741:20: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglDataF(mglDataF &&d):nx(d.nx),ny(d.ny),nz(d.nz), str(d.str), v1(d.v1),v2(d.v2), ex(d.ex), dx(d.dx),dy(d.dy),dz(d.dz), dfunc(d.dfunc),par(d.par)
                    ^
/usr/include/mgl2/data.h:741:23: error: invalid constructor; you probably meant ‘mglDataF (const mglDataF&)’
  mglDataF(mglDataF &&d):nx(d.nx),ny(d.ny),nz(d.nz), str(d.str), v1(d.v1),v2(d.v2), ex(d.ex), dx(d.dx),dy(d.dy),dz(d.dz), dfunc(d.dfunc),par(d.par)
                       ^
/usr/include/mgl2/data.h:846:20: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglDataT(mglDataT &&d):dat(d.dat),ind(d.ind)
                    ^
/usr/include/mgl2/data.h:846:23: error: invalid constructor; you probably meant ‘mglDataT (const mglDataT&)’
  mglDataT(mglDataT &&d):dat(d.dat),ind(d.ind)
                       ^
/usr/include/mgl2/data.h:893:20: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglDataR(mglDataR &&d):dat(d.dat),ind(d.ind)
                    ^
/usr/include/mgl2/data.h:893:23: error: invalid constructor; you probably meant ‘mglDataR (const mglDataR&)’
  mglDataR(mglDataR &&d):dat(d.dat),ind(d.ind)
                       ^
In file included from /usr/include/mgl2/mgl_cf.h:25:0,
                 from /usr/include/mgl2/mgl.h:23,
                 from /usr/include/mgl2/wnd.h:23,
                 from /usr/include/mgl2/qt.h:36,
                 from sample.cpp:1:
/usr/include/mgl2/datac.h:50:20: error: expected ‘,’ or ‘...’ before ‘&&’ token
  mglDataC(mglDataC &&d):nx(d.nx),ny(d.ny),nz(d.nz),a(d.a),id(d.id),link(d.link)
                    ^
/usr/include/mgl2/datac.h:50:23: error: invalid constructor; you probably meant ‘mglDataC (const mglDataC&)’
  mglDataC(mglDataC &&d):nx(d.nx),ny(d.ny),nz(d.nz),a(d.a),id(d.id),link(d.link)
                   ^

因此,我希望使用dict <Grid> <DatePicker Date="{Binding FundraiserEndDate}" /> <Entry Text="{Binding FundraiserEndDateText}" TextColor="Gray" FontSize="13" HorizontalTextAlignment="Center" InputTransparent="True" /> </Grid> 中的某些数据更新user_created = User.objects.create_user(first_name="Wally", username="farseer@gmail.com", password="1234", email="farseer@gmail.com") client_created = Client.objects.create(user=user_created, cart=cart) data_client_profile["user"]["first_name"] = "Apoc" response = self.client.put(reverse("misuper:client_profile"), data_client_profile, format="json") client_created.refresh_from_db() # Tried this too self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(client_created.user.first_name, data_client_profile["user"]["first_name"]) 对象,然后client_created更新data_client_profile是“Apoc”。

以下是视图中的代码,我添加了两个assertEqual,这不仅有助于粘贴所有代码:

client.user.first_name

第一个pdb中断显示了这个:

pdb.set_trace()

第二个pdb中断显示:

        pdb.set_trace()

        client_existing_user_obj.phone = phone
        client_existing_user_obj.user.email = email
        client_existing_user_obj.user.first_name = first_name # Updating here!
        client_existing_user_obj.user.last_name = last_name
        client_existing_user_obj.user.save()
        client_existing_user_obj.save()
        pdb.set_trace()

但是,当测试运行时,我得到错误:

(Pdb) client_existing_user_obj.user.username
u'farseer@gmail.com'  # Make sure I'm updating the created object
(Pdb) client_existing_user_obj.user.first_name
u'Wally'  # First name is not updated yet

为什么会失败?我甚至打电话给(Pdb) client_existing_user_obj.user.first_name u'Apoc' # Looks like the first name has being updated! 。我确认它已在视图中更新,但在测试中它看起来没有。我不明白。

2 个答案:

答案 0 :(得分:2)

您需要从数据库刷新用户,因为这是您正在修改的对象:

user_created.refresh_from_db()

答案 1 :(得分:1)

请注意,refresh_from_db的文档表示client_created.user不会刷新client_created.refresh_from_db(),因为client_created.user_id保持不变:

  

从重新加载的实例中删除以前加载的关系值不再有效的相关实例。例如,如果您有一个从重新加载的实例到另一个名为Author的模型的外键,那么如果obj.author_id != obj.author.idobj.author将被丢弃,并且在下次访问时它将被重新加载值为obj.author_id

因此您需要刷新client_created.user

client_created.user.refresh_from_db()

或自己重新提取client_created

client_created = Client.objects.get(pk=client_created.pk)