小编Mel*_*eir的帖子

Django单元测试客户端响应有空上下文

我有一个单元测试,它在一个断言中失败,该断言在同一测试用例类中传递了另一个测试.

这是通过测试:

def test_home(self):
    c = Client()
    resp = c.get('/')
    self.assertEqual(resp.status_code, 200)
    self.assertTrue('a_formset' in resp.context)
Run Code Online (Sandbox Code Playgroud)

这是失败的测试:

def test_number_initial_number_of_forms(self):
    c = Client()
    resp = c.get('/')
    self.assertEqual(resp.context['a_formset'].total_form_count(), 1)
Run Code Online (Sandbox Code Playgroud)

在第二次测试中,我得到了错误TypeError: 'NoneType' object has no attribute '__getitem__'.

如果我执行第二次测试

def test_number_initial_number_of_forms(self):
    c = Client()
    resp = c.get('/')
    self.assertTrue('a_formset' in resp.context)
    self.assertEqual(resp.context['a_formset'].total_form_count(), 1)
Run Code Online (Sandbox Code Playgroud)

我收到了错误TypeError: argument of type 'NoneType' is not iterable.我在第二次测试中通过print语句确认了response.content包含我希望得到的页面,状态代码是否正确,以及模板是否正确.但是响应的背景始终None是第二次测试.

我通过标准的"python manage.py test ..."界面运行我的Django单元测试,所以我不相信我遇到了" 上下文是空的shell "问题.

这是怎么回事?

编辑:

如果我添加print type(resp.context['a_formset'])到每个测试,我得到的工作测试<class 'django.forms.formsets.AFormFormSet'>.对于非工作测试,我TypeError: 'NoneType' object has …

python django django-unittest

13
推荐指数
2
解决办法
4423
查看次数

使用JavaScript insertBefore()在TextNode之前插入?

我有以下HTML:

<div id="move-me">
    <a href="#">I'm a link</a>
</div>

<div id="new-parent">
    Some plain text.
</div>
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写JavaScript,将整个#move-me div移到文本上方的#new-parent div中,如下所示:

<div id="new-parent">
    <div id="move-me">
        <a href="#">I'm a link</a>
    </div>
    Some plain text.
</div>
Run Code Online (Sandbox Code Playgroud)

这是我的JavaScript:

function moveDiv() {
    var moveable = document.getElementById('move-me');
    var newParent = document.getElementById('new-parent');
    newParent.parentNode.insertBefore(moveable, newParent.firstChild);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Firebug进行调试,我可以看到它newParent.firstChild是一个TextNode,但我总是收到以下错误:

Node was not found" code: "8
newParent.parentNode.insertBefore(moveable, newParent.firstChild); 
Run Code Online (Sandbox Code Playgroud)

它似乎insertBefore需要一个元素节点,不能在文本节点上工作......是吗?如果是这样,还有另一个好方法吗?

注意:我无法修改或清理HTML以包含段落标记或删除空格.

javascript dom

12
推荐指数
1
解决办法
1万
查看次数

Fabric无法下载设置Error Domain = FABNetworkError Code = -6

我在我的应用程序中实现了Fabric,它始终正常工作.现在,当我尝试在设备上运行我的应用程序时,我收到以下错误:

[Fabric]无法下载设置Error Domain = FABNetworkError Code = -6"(null)"

在模拟器上始终正常工作.任何的想法?

xcode objective-c ios twitter-fabric

10
推荐指数
1
解决办法
7278
查看次数

如何将图像添加到布料画布?

我想在我的布料画布上添加图像/图标.Fabric演示中提供的代码无效.

fabric.Image.fromURL('my_image.png', function(oImg) {
canvas.add(oImg);
});
Run Code Online (Sandbox Code Playgroud)

这只是让我的整个画布空白.我想添加图标作为响应事件的可点击元素.

html javascript canvas fabricjs

10
推荐指数
2
解决办法
2万
查看次数

单元测试django内联formset

在尝试以编程方式通过内联formset发布一个新的ForeignKey对象时,我收到一个错误:ValueError: invalid literal for int() with base 10: ''.

这是我的测试代码(为了简洁而臃肿):

def test_merits_can_be_added(self):
    self.c = Client()
    self.c.login(username=self.user.username, password='dummy')
    self.post_data = {
      'name':u'Unhappy SE',
      'concept':u'Sad clown',
      'merit-TOTAL_FORMS':u'1',
      'merit-MAX_NUM_FORMS':u'',
      'merit-INITIAL_FORMS':u'1',
      'merit-0-id':u'',
      'merit-0-level':u'2',
      'merit-0-character':u'1',
      'merit-0-trait':u'11',
      'merit-0-specializations':u'Sometimes'
    }
    sheet = GeistCharacterSheet.objects.create(name='Happy SE', user=self.user)
    response = self.c.post(sheet.get_absolute_url(), self.post_data, follow=True)
    self.assertEqual(GeistCharacterSheet.objects.get(pk=1).chosentrait_set.all().filter(trait__name='Common Sense')[0].level, 2)
    self.assertEqual(GeistCharacterSheet.objects.get(pk=1).chosentrait_set.all().filter(trait__name='Common Sense')[0].specializations, u'Sometimes')
Run Code Online (Sandbox Code Playgroud)

视图代码(为简洁起见,再次修剪):

def character_sheet(request, sheet_id=None):
  charsheet = GeistCharacterSheet.objects.get(pk=sheet_id, user=request.user)
  if request.method == 'POST':
    sheet_form = GeistCharacterSheetForm(request.POST, instance=charsheet)          
    merit_formset = setup_merit_form(charsheet, post=request.POST)

    if sheet_form.is_valid() and merit_formset.is_valid():
      sheet_form.save()
      merit_formset.save()
      return redirect('/character-manager/list/')

def …
Run Code Online (Sandbox Code Playgroud)

django django-forms inline-formset

9
推荐指数
1
解决办法
2269
查看次数

使用WinForms的Humble View/MVP和UserControls的集合

我正在重构一个WinForms(.NET 4)应用程序,该应用程序使用TabControl来包含UserControl - UserControl在每个TabPage中实例化,最终结果是每个选项卡中的编辑器.这些正在编辑一组项目,这些项目最终会被整个表单输入到正在编辑的对象中.

作为示例类结构:

  • class School
    • string Name
    • string Address
    • Course,每个具有几个相应的字段(Department,Name等)

(它实际上不是与学校相关的应用程序,但这个比喻有效.)

在视觉上,UserControls集管理Coursees,而父Form处理School信息.

现在,我有一个Form/School的演示者,以及UserControl/Course的演示者,每个人都有一个视图.然而,学校的主持人需要控制课程的一些信息.例如,为一个课程选择的选项限制其他课程中的选项.该School模型正在处理该计算,但它需要到达课程的演示者.

我在MVP讨论中找到这种关系的例子并没有太大的成功,这是我第一次采用MVP方法.处理这个有什么好办法吗?学校的主持人是否适合收集课程主持人的集合来代表该集?学校的观点是否应该收集课程观点的集合?(最终的UserControls必须以某种方式和某个地方附加到表单上,对吧?)

我的主要目标是(不出所料)提高可测试性和可维护性,到目前为止,这个过程的主要来源是Michael Feathers的"The Humble Dialog Box"和Jeremy Miller的"Build You Own CAB"系列.

.net mvp passive-view supervisingcontroller winforms

5
推荐指数
1
解决办法
1003
查看次数

如何为 DynamicResource 使用 ViewModel 变量?

这不是 MahApps.Metro 特定的,但这恰好是我正在使用的。我有一组 ViewModel,它们有一个string属性,表示要使用资源 XAML 文件中的哪个图标。

public class CommandViewModel : ViewModel
{
    public CommandViewModel(string displayName, ICommand command, string icon)
    {
        if (command == null)
            throw new ArgumentNullException("command");

        DisplayName = displayName;
        Command = command;
        Icon = icon;
    }

    public ICommand Command { get; private set; }
    public string Icon { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Icon最终会成为 MahApps.Metro.Resources 中的“appbar_add”之类的东西。这些是在 Icons.xaml 文件中定义的。

我如何在我的ItemTemplate这样的文件中写出正确的资源显示。我要么在执行时出错(不是在编辑/构建时),要么根本没有图标。

“无图标”XAML 如下所示:

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Rectangle Width="20" Height="20">
                    <Rectangle.Fill>
                        <VisualBrush Visual="{DynamicResource {Binding Path=Icon}}" …
Run Code Online (Sandbox Code Playgroud)

wpf xaml

5
推荐指数
1
解决办法
2298
查看次数

在哪里模拟测试调用服务的Django视图?

这是Django 1.6.8,Python 2.7和模拟库.

我有一个视图,使用suds调用远程服务销售税信息(这是一个简化版本):

def sales_tax(self, bundle_slug):
    bundle = get_object_or_404(Bundle, slug=bundle_slug,
                                active=True)

    cloud = TaxCloud(TAXCLOUD_API_ID, TAXCLOUD_API_KEY)
    origin = Address('origin address')
    destination = Address('customer address')
    cart_item = CartItem(bundle.sku, TAXCLOUD_TIC_ONLINE_GAMES,
                        bundle.price, 1)

    try:
        rate_info = cloud.get_rate(origin, destination,
                                    [cart_item],
                                    str(customer.id))

        sales_tax = Decimal(rate_info['sales_tax'])

        response = {'salesTax': locale.currency(sales_tax),
                    'total':  locale.currency(bundle.price + sales_tax)}
    except TaxCloudException as tce:
        response = {
            'error': str(tce)
        }
Run Code Online (Sandbox Code Playgroud)

以下是该TaxCloud课程的相关代码:

from suds.client import Client
from suds import WebFault

class TaxCloud(object):

    def __init__(self, api_login_id, api_key):
        self.api_login_id = api_login_id …
Run Code Online (Sandbox Code Playgroud)

python django python-mock

5
推荐指数
0
解决办法
878
查看次数

Android twitter登录与面料集成,点击白屏然后关闭白屏

我正在尝试使用fabric在android studio中设置twitter登录.一切都很好地构建,没有任何错误.

当我尝试在手机上运行应用程序并按下Twitter登录按钮时,它会进入白色屏幕并显示进度条,然后关闭并返回到带有twitter按钮的屏幕.它不会使应用程序崩溃,它只会给我一个错误.

我完全遵循了twitters开发人员文档.这是我得到的错误

01-25 16:58:59.359 32491-32491/com.myegotest.ego_17012016 E/Twitter: SSO auth activity not found
01-25 16:58:59.369 827-1358/? E/Parcel: Class not found when unmarshalling: com.twitter.sdk.android.core.TwitterAuthConfig
01-25 16:58:59.369 827-1358/? E/Parcel: java.lang.ClassNotFoundException: com.twitter.sdk.android.core.TwitterAuthConfig
01-25 16:58:59.369 827-1358/? E/Parcel:  Caused by: java.lang.NoClassDefFoundError: com/twitter/sdk/android/core/TwitterAuthConfig
01-25 16:58:59.369 827-1358/? E/Parcel:  Caused by: java.lang.ClassNotFoundException: Didn't find class "com.twitter.sdk.android.core.TwitterAuthConfig" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
01-25 16:59:00.399 32491-32491/com.myegotest.ego_17012016 E/Twitter: Invalid json: <?xml version="1.0" encoding="UTF-8"?>
01-25 16:59:00.399 32491-32491/com.myegotest.ego_17012016 E/Twitter: <hash>
01-25 16:59:00.399 32491-32491/com.myegotest.ego_17012016 E/Twitter:   <error>Desktop applications only support the oauth_callback value 'oob'</error>
01-25 …
Run Code Online (Sandbox Code Playgroud)

java api twitter android twitter-fabric

5
推荐指数
1
解决办法
1437
查看次数

错误:原因:未在build.grade中指定buildToolsVersion(项目:xxx)

现在我正在尝试将Fabric与Crashlytics(https://docs.fabric.io/android/fabric/integration.html)集成到现有的Android应用程序中.

按照Fabric提供的集成指令,我将添加apply plugin: 'com.android.application'到"build.grade(Project.NumenuApp)"文件的根目录中.我点击同步Gradle,它失败,我收到此错误Error:Cause: buildToolsVersion is not specified.Android工作室声明我的插件应该在Module而不是Project级别声明.

我已经在"build.grade(Module:app)"文件中设置了buildTools.我的问题是为什么会发生这种情况,我该怎么做才能解决这个问题?我在下面发布了我的代码.提前致谢. - 阿德里安

build.grade(项目:NumenuApp)

// Top-level build file where you can add configuration options common to all     sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

allprojects {
    repositories {
        jcenter() …
Run Code Online (Sandbox Code Playgroud)

configuration android build-tools gradle twitter-fabric

2
推荐指数
1
解决办法
2万
查看次数