小编jz9*_*999的帖子

我如何测试Django QuerySets是否相等?

我正在尝试测试我的Django视图.此视图将QuerySet传递给模板:

def merchant_home(request, slug):
  merchant = Merchant.objects.get(slug=slug)
  product_list = merchant.products.all()
  return render_to_response('merchant_home.html',
                            {'merchant': merchant,
                            'product_list': product_list},
                            context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

并测试:

  def test(self):
    "Merchant home view should send merchant and merchant products to the template"
    merchant = Merchant.objects.create(name='test merchant')
    product = Product.objects.create(name='test product', price=100.00)
    merchant.products.add(product)

    test_client = Client()
    response = test_client.get('/' + merchant.slug)
    # self.assertListEqual(response.context['product_list'], merchant.products.all())
    self.assertQuerysetEqual(response.context['product_list'], merchant.products.all())
Run Code Online (Sandbox Code Playgroud)

编辑 我用的是self.assertQuerysetEqual而不是self.assertListEqual.不幸的是,这仍然无效,终端显示: ['<Product: Product object>'] != [<Product: Product object>]


assertListEqual提出:'QuerySet' object has no attribute 'difference'并且 assertEqual也不起作用,虽然self.assertSetEqual(response.context['product_list'][0], …

python django django-queryset django-testing

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

如何将多个Web音频源/轨道压缩为一个?

我们正在制作基于Web Audio api的基于Web的音乐编辑器和混音器.用户可以将多个轨道,裁剪轨道等混合在一起.轨道的实际混合仅涉及一次播放所有源.

我们希望能够添加选项以保存混音并使其可供下载到用户的计算机.有没有办法在前端执行此操作(如将所有源连接到一个目标/导出节点),甚至后端(我们使用的是RoR)?

javascript audio google-chrome html5-audio web-audio-api

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

为什么Ruby中的&&有时候是快捷方式评估,有时候不是?

我想测试散列中的元素是否存在以及是否> = 0,然后将true或false放入数组中:

boolean_array << input['amount'] && input['amount'] >= 0
Run Code Online (Sandbox Code Playgroud)

这在NilClass错误上引发no> =.但是,如果我这样做:

input['amount'] && input['amount'] >= 0   #=> false
Run Code Online (Sandbox Code Playgroud)

没问题.基本上:

false && (puts 'what the heck?') #=> false
arr = []
arr << false && (puts 'what the heck?') #=> stdout: 'what the heck?'
arr #=> [false]
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

ruby

2
推荐指数
3
解决办法
79
查看次数

条件"if"修饰符中的Ruby变量赋值

我有一个关于Ruby解释器如何分配变量的问题:

我经常使用这个:

return foo if (foo = bar.some_method)
Run Code Online (Sandbox Code Playgroud)

some_method返回一个对象或nil.

但是,当我尝试这个:

return foo if (true && (foo = bar.some_method))
Run Code Online (Sandbox Code Playgroud)

我得到:NameError:未定义的局部变量或方法foo for main:Object.

导致第二行出错的第一行和第二行之间的评估有何不同?

ruby if-statement local-variables variable-assignment

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

删除对象不会更新内存中已存在的关联

我们正在使用Rails 3.2.5

这是我们使用的代码:

class MR < ActiveRecord::Base
  has_many :codes

  def test
    codes.each { |c| c.delete }
  end

  def asdf
    codes.size
  end
end
Run Code Online (Sandbox Code Playgroud)

如果我这样称呼:

mr = MR.create 
# imagine mr has 5 codes
mr.test
# confirmed that 5 codes have been deleted from database using Sequel Pro
mr.asdf => 5
mr.reload.asdf => 0
Run Code Online (Sandbox Code Playgroud)

删除其中的对象时,是否总是必须重新加载关联?我应该使用不同的方法吗?我以为毁灭会这样做,但它不能解决问题.

ruby activerecord ruby-on-rails

0
推荐指数
1
解决办法
66
查看次数