今天就来说说 expect()
与 expect {}
的差别吧
我们用一个例子来讲
describe 'Post #create' do
let(:params_collection) { { collection: { title: '11', description: '222' } } }
let(:create_collection) { post '/collections', params: params_collection }
it 'redirect to index when create collection success' do
create_collection
expect(Collection.count).to eq 1
end
end
当我们打 post create 时会新建出一个 collection 且会期望会使 Collection
的数量增加 1
如果改成 block 的写法就会像是
it 'redirect to index when create collection success' do
expect{ create_collection }.to change { Collection.count }.by(1)
end
其实就是在告诉 expect
欸你 block 里面的create_collection
做完之後帮我看一下我 collection 的数量有没有加 1
describe 'Get #show' do
let(:collection) { create(:collection) }
it 'redirect to index when create collection success' do
expect { get "/collections/1" }.to raise_error(ActiveRecord::RecordNotFound)
end
也可以用{}
去观察当我打了get "/collections/1"
的时候应该要起error而且是ActiveRecord::RecordNotFound
因为我连实体都还没建立出来,怎麽可能会找得到collection
那我们就会知道
expect()
=> 当你只想验证回传一个值的时候就用 ()
expect{}
=> 当你期望 block 做事情的这段内会发生哪些变化时就该使用 {}
学习新知的第一步是初步了解原理,学习新的开发技术除了原理也要先学习设定环境。 写在最最最前面 虽然N...
一般来说, 如果要在Odoo原生资料表增加栏位, 只要在py里写好就好, 像是这样 上图是Many2...
本篇章主要是先从技术层面来解释 URI 与 URL 及比较少使用到却与生活息息相关的 URN,让读者...
django 基础篇 基本工具 env虚拟机 - virtualenvwrapper-win 安装:...
当拿到资料之後,我通常的第一步一定是先看一下这笔资料大致长什麽样子 这个时候有了能统计每个类别出现了...