我正在尝试测试我的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], …
我们正在制作基于Web Audio api的基于Web的音乐编辑器和混音器.用户可以将多个轨道,裁剪轨道等混合在一起.轨道的实际混合仅涉及一次播放所有源.
我们希望能够添加选项以保存混音并使其可供下载到用户的计算机.有没有办法在前端执行此操作(如将所有源连接到一个目标/导出节点),甚至后端(我们使用的是RoR)?
我想测试散列中的元素是否存在以及是否> = 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解释器如何分配变量的问题:
我经常使用这个:
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.
导致第二行出错的第一行和第二行之间的评估有何不同?
我们正在使用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 ×3
activerecord ×1
audio ×1
django ×1
html5-audio ×1
if-statement ×1
javascript ×1
python ×1