桃皮绒是什么面料| 海鲜不能和什么水果一起吃| 长痘痘涂什么药膏| 什么睡姿对髋关节好| 内外兼修是什么意思| 负荆请罪的负是什么意思| 发烧嗓子疼吃什么药好| 牛鞭是牛的什么部位| 熊猫血是什么血型| 一天当中什么时候血压最高| 脂肪肝挂什么科室| 表现优异是什么意思| 2008属什么生肖| 脉络是什么意思| 子宫位于腹部什么位置| iu是什么单位| 宀与什么有关| 脚趾头长痣代表什么| 豇豆不能和什么一起吃| 胃疼吃什么食物对胃好| 水瓶座后面是什么星座| 什么是梦想| 53年属什么| 怀孕第一天有什么症状| 什么叫自负| 维生素b12又叫什么| 什么体质不易怀孕| 为什么来我家| 虾不能和什么东西一起吃| 三姓家奴是什么意思| 泌尿科看什么病| 四相是什么意思| 受凉吃什么药| 梅花三弄的三弄指什么| 长期吃面条对身体有什么影响| 纹银是什么意思| h 是什么意思| 陈醋和蜂蜜一起喝有什么好处| h1v是什么病| 生理需要是什么意思| 中焦不通用什么中成药| 尖嘴猴腮是什么生肖| 阉鸡是什么鸡| 乙肝五项一五阳性什么意思| 为什么睡觉会磨牙| 2月25号是什么星座| 慢性荨麻疹是什么症状| 谛听是什么意思| 平板电脑与笔记本电脑有什么区别| 什么是兼职| 什么海翻江| 腋毛上有白色附着物是什么| 周杰伦什么病| 清宫后需要注意什么| 11月14号是什么星座| 彩虹旗是什么意思| 低密度脂蛋白偏高吃什么药| 血管堵塞有什么症状| 人体最大的排毒器官是什么| 周瑜为什么打黄盖| 山竹树长什么样子图片| alt医学上是什么意思| 什么口罩| 平均血小板体积偏高是什么原因| butterfly是什么意思| 附子是什么| 慰藉是什么意思| 清热解毒是什么意思| 硬膜囊前缘受压是什么意思| 木可以加什么偏旁| lpl是什么| 5月4日是什么星座| 肠道蠕动慢吃什么药| 下雨为什么会打雷闪电| 重阳节的习俗是什么| 小孩喜欢趴着睡觉是什么原因| 什么是鼻炎| msms筛查是什么意思| 脚背浮肿是什么原因引起的| 径行是什么意思| 变更是什么意思| 霉菌性阴炎用什么药止痒效果好| 烧心吃什么马上能缓解| 邓紫棋为什么叫gem| ic50是什么意思| 李子与什么食物相克| 梦见白事场面什么意思| 经常饿是什么原因| 红参适合什么人吃| 孩子专注力差去什么医院检查| 柠檬水喝多了有什么坏处| 波尔多红是什么颜色| 辅助治疗是什么意思| 小苏打和柠檬酸反应产生什么| 梨花是什么颜色| 重楼的别名叫什么| 牙齿黑是什么原因| 经常头晕头疼是什么原因| 国保大队是干什么的| 手热脚热是什么原因| 六味地黄丸有什么作用| 梦见生肉是什么征兆| 一帘幽梦是什么意思| 喝什么利尿效果最好| 太上皇是什么意思| 摩羯座什么时候| guou是什么牌子的手表| 屎忽鬼是什么意思| 近五行属什么| 参军意愿选什么比较好| 四个木是什么字| studio什么牌子| 去离子水是什么| 梦到自己拔牙齿是什么预兆| 过午不食是什么意思| 职场是什么意思| 分泌物豆腐渣状是什么原因| 煲蛇汤放什么材料好| 拉肚子吃什么药好使| 韩语欧巴是什么意思| 什么什么大叫| 脑血管造影是什么意思| 九月是什么星座| 七月11日是什么星座| 法国铁塔叫什么名字| 做梦梦到鸡是什么意思| 什么学习机好| 餐后血糖高是什么原因| 孩子鼻子流鼻血是什么原因| 肾上腺挂什么科| 什么人一年只工作一天| 移民瑞士需要什么条件| 办离婚证需要带什么证件| 太原有什么特产| 2023年是属什么生肖| 云州是现在的什么地方| 籽字五行属什么| 勤对什么| 肚子疼吃什么药| 家父是什么意思| 长针眼是什么原因| 为什么明星不激光祛斑| 女人脸黄是什么原因该怎么调理| 勾心斗角是什么生肖| 霖五行属性是什么| 组织部副部长是什么级别| b型钠尿肽是什么意思| 磷偏低是什么原因| 央企和国企有什么区别| 什么叫戒断反应| 包二奶是什么意思| 舒坦是什么意思| 泡菜生花用什么方法可以去掉| 钟馗是什么意思| 血尿是什么颜色| ab型和o型生的孩子是什么血型| 什么声什么气| 渣渣辉什么意思| 日语牙白什么意思| 海鲜菇不能和什么一起吃| 过午不食是什么意思| 神经损伤是什么症状| 肺部小结节是什么意思| 女生的下面叫什么| 领养孩子需要什么条件| 儿童便秘吃什么最管用| 本卦和变卦是什么关系| pt指什么| 食物中毒吃什么药| 属猴女和什么属相最配| 香菜什么时候种最合适| 办电话卡需要什么| 什么水果去火| 小马过河的故事告诉我们什么道理| 睡前一杯牛奶有什么好处| 体重除以身高的平方是什么指数| 什么含维生素d| 消融手术是什么意思| 撒野是什么意思| 内瘘是什么意思| 薛字五行属什么| 胃酸过多吃什么| 梅花表属于什么档次| 未亡人什么意思| 体温偏高的人说明什么| 肾虚吃什么食物| 来月经腰疼是什么原因| 两票制指的是什么| 珮字五行属什么| 铁树开花是什么意思| 政协是干什么的| 手抖是什么病的前兆| 补脑吃什么食物| 孕妇吃什么血糖降得快| 一阵什么| 煜什么意思| 250为什么是骂人的话| 减肥可以吃什么肉| 尿毒症可以吃什么水果| 襁褓是什么意思| 柏字五行属什么| 红是什么生肖| 脸上出汗多是什么原因| 什么体质的人才有季经| 妨夫痣是什么意思| 早上起来后背疼是什么原因| 移植后要注意什么| 猪头肉是什么意思| 成家是什么意思| 什么而去| 姑娘是什么意思| 喉咙疼痛吃什么药| 什么叫四大皆空| 世界八大奇迹分别是什么| blossom是什么意思| 火腿是什么动物的腿| 经常玩手机有什么危害| 手掌心经常出汗是什么原因| 什么是低聚果糖| 感性的人是什么意思| 拉红色的屎是什么原因| 身体容易青紫是什么原因| 经常嗓子疼是什么原因| 2008年属鼠是什么命| 心率快吃什么药效果更佳| 黄桃什么时候上市| 猪头肉是什么意思| 馥是什么意思| 1月19日什么星座| 人生得意须尽欢是什么意思| 前额头痛吃什么药| 房奴什么意思| 出库是什么意思| 258什么意思| 大出血是什么症状| 晚上十一点多是什么时辰| 海蛎子是什么| 什么实实| 事业编制是什么意思| 心管是什么部位| 为什么地球是圆的| 什么是高原反应| 11月份是什么星座| 5月14日什么星座| 晚上喝柠檬水有什么好处| cd3cd4cd8都代表什么| 茶花什么时候开花| 什么是贡菜| 胎盘厚度代表什么| 尿酸高吃什么好| 推介是什么意思| 蜈蚣咬了擦什么药最好| 你想要什么我都会给| 疤痕增生是什么样子| 晚饭吃什么| 穷的生肖指什么生肖| 超度什么意思| 清水是什么意思| mgd是什么意思| 对乙酰氨基酚片是什么药| 蓝莓是什么季节的水果| joeone是什么牌子| 软组织感染是什么意思| 今天拉警报什么意思| 百度
Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing 201, or: Great Expectations

国务院安委会安全生产巡查组对我厅开展延伸巡查

百度 可以说,阿兰本赛季的大爆发成为了恒大意外惊喜,他让恒大拥有两位超级前锋,高拉特身上的压力也小了很多。

Ruby developers are a testy bunch. Who else writes more tests that we do? Which makes sense! Our language allows us freedom, and the opportunity to abuse it.

Most of us learned to test informally, from existing projects or tutorials. Sadly, that often leads us to the false conclusion that is all about verifying that our code works. Would you believe that’s only the third most important reason to test?

Let’s take a non-dogmatic look at the less obvious roles for testing with some real examples. We can get so much more from testing.

Avatar for Joseph Mastey

Joseph Mastey

August 04, 2020
Tweet

More Decks by Joseph Mastey

Other Decks in Programming

Transcript

  1. class CreateShipmentHistoryReport attr_accessor :user, :shipments def initialize(user) @user = user

    @shipments = user.shipments end def process shipments.sort_by(&:created_at) data = shipments.map do |shipment| shipment.to_json.values + [ shipment.user.name, shipment.product.name ] end file = CSV.open(tmp_filename) do |csv| csv << shipments.first.to_json.keys + ['user', 'product'] data.map do |row| csv << row end end persist_csv_to_s3(file) notify_user(user.email) file end end
  2. def process shipments.sort_by(&:created_at) data = shipments.map do |shipment| shipment.to_json.values +

    [ shipment.user.name, shipment.product.name ] end # continued below...
  3. # ... process continued file = CSV.open(tmp_filename) do |csv| csv

    << shipments.first.to_json.keys + ['user', 'product'] data.map do |row| csv << row end end persist_csv_to_s3(file) notify_user(user.email) file end
  4. class CreateShipmentHistoryReport attr_accessor :user, :shipments def initialize(user) @user = user

    @shipments = user.shipments end def process shipments.order('created_at desc') data = shipments.map do |shipment| shipment.to_json.values + [ shipment.user.name, shipment.product.name ] end file = CSV.open(tmp_filename) do |csv| csv << shipments.first.to_json.keys + ['user', 'product'] data.map do |row| csv << row end end persist_csv_to_s3(file) notify_user(user.email) file end end
  5. describe CreateShipmentHistoryReport do describe "#process" do # ... it "generates

    an array of strings" do expect(csv_data.length).to eq(shipments.length) expect(csv_data).to all(be_an(Array)) end it "matches the expected output" do expect(csv_data).to eq(output_records) end end end
  6. let(:user) { create(:user, :signup_complete) } let(:subscription) { create(:subscription) } before

    do user.subscriptions << subscription expect(user) .to receive(:shipments) .and_return(shipments) end
  7. let!(:shipment_1) { create(:shipment, product: product_1, created_at: 2.days.ago) } let!(:shipment_2) {

    create(:shipment, product: product_2, created_at: 3.days.ago) } let!(:shipment_3) { create(:shipment, product: product_3, created_at: 4.days.ago) } let!(:shipment_4) { create(:shipment, product: product_4, created_at: 5.days.ago) }
  8. let(:output_records) do [ [shipment_4.created_on, shipment_4.completed_on, user.name, product_4.name], [shipment_3.created_on, shipment_3.completed_on, user.name,

    product_3.name], [shipment_2.created_on, shipment_2.completed_on, user.name, product_2.name], [shipment_1.created_on, shipment_1.completed_on, user.name, product_1.name], ] end
  9. describe CreateShipmentHistoryReport do describe "#process" do let(:user) { create(:user, :signup_complete)

    } let(:subscription) { create(:subscription) } let(:product_1) { create(:product) } let(:product_2) { create(:product) } let(:product_3) { create(:product) } let(:product_4) { create(:product) } let!(:shipment_1) { create(:shipment, product: product_1, created_at: 2.days.ago) } let!(:shipment_2) { create(:shipment, product: product_2, created_at: 3.days.ago) } let!(:shipment_3) { create(:shipment, product: product_3, created_at: 4.days.ago) } let!(:shipment_4) { create(:shipment, product: product_4, created_at: 5.days.ago) } let(:subject) { described_class.new(user).process } let(:csv_data) { CSV.parse(subject) } let(:shipments) do [ shipment_1, shipment_2, shipment_3, shipment_4 ] end let(:output_records) do [ [shipment_4.created_on, shipment_4.completed_on, user.name, product_4.name], [shipment_3.created_on, shipment_3.completed_on, user.name, product_3.name], [shipment_2.created_on, shipment_2.completed_on, user.name, product_2.name], [shipment_1.created_on, shipment_1.completed_on, user.name, product_1.name], ] end before do user.subscriptions << subscription expect(user) .to receive(:shipments) .and_return(shipments) expect_any_instance_of(described_class) .to receive(:save_to_s3) .and_return(true) expect_any_instance_of(described_class) .to receive(:email_user) .and_return(true) end it "generates an array of strings" do expect(csv_data.length).to eq(shipments.length) expect(csv_data).to all(be_an(Array)) end it "matches the expected output" do expect(csv_data).to eq(output_records) end end end
  10. let(:csv_data) { CSV.parse(subject) } before do expect_any_instance_of(described_class) .to receive(:save_to_s3) .and_return(true)

    expect_any_instance_of(described_class) .to receive(:email_user) .and_return(true) end
  11. def data(shipments: @shipments) shipments.sort_by!(&:created_at) shipments.map do |shipment| shipment.to_json.values + [

    shipment.user.name, shipment.product.name ] end end def headers shipments.first.to_json.keys + ['user', 'product'] end
  12. before do # expect_any_instance_of(described_class) # .to receive(:save_to_s3) # .and_return(true) #

    expect_any_instance_of(described_class) # .to receive(:email_user) # .and_return(true) end
  13. def data(shipments) shipments.sort_by!(&:created_at) shipments.map do |shipment| shipment.to_json.values + [ shipment.user.name,

    shipment.product.name ] end end def headers shipments.first.to_json.keys + ['user', 'product'] end
  14. describe "#serialize" do it "serializes some shipment and user data"

    do shipment = create(:shipment) report = described_class.new(user, []) result = report.serialize(shipment) expect(result).to eq({ created_on: shipment.created_on, completed_on: shipment.completed_on, user: shipment.user.name, product: shipment.product.name, }) end end
  15. # let(:output_records) do # [ # [shipment_4.created_on, shipment_4.completed_on, # user.name,

    product_4.name], # [shipment_3.created_on, shipment_3.completed_on, # user.name, product_3.name], # [shipment_2.created_on, shipment_2.completed_on, # user.name, product_2.name], # [shipment_1.created_on, shipment_1.completed_on, # user.name, product_1.name], # ] # end
  16. describe "ordering" do it "reorders shipments by their creation date"

    do report = described_class.new(user,[shipment_1, shipment_2]) result = report.data expect(result.map(&:first)).to eq( [shipment_2.created_on, shipment_1.created_on] ) end end
  17. Factory AR records AR queries create(:user) 1 10 create(:meal) 5

    41 create(:full_menu) 94 584 create(:weekly_basket) 104 644 create(:user, :with_order_history) 379 2336
  18. describe "ordering" do it "reorders shipments by their creation date"

    do report = described_class.new(user, [shipment_1, shipment_2]) result = report.data expect(result.map(&:first)).to eq( [shipment_2.created_on, shipment_1.created_on] ) end end
  19. describe "ordering" do it "reorders shipments by their creation date"

    do old = create_shipment(date: 9.days.ago) new = create_shipment(date: 2.days.ago) report = described_class.new(user, [new, old]) result = report.data expect(result.map(&:first)).to eq( [old.created_on, new.created_on] ) end end
  20. it "serializes users as passed into the service" do users

    = create_list(:user, 3) response = subject.new(User.all).process response = response[0][4] expect(response).to eq(users.first.name) end
  21. it "serializes users as passed into the service" do users

    = create_list(:user, 3) serialized_users = subject.new.serialize(users) response = serialized_users.first.full_name expect(response).to eq(users.first.name) end
  22. let!(:shipment_1) { create(:shipment, product: product_1, created_at: 2.days.ago) } let!(:shipment_2) {

    create(:shipment, product: product_2, created_at: 3.days.ago) } let!(:shipment_3) { create(:shipment, product: product_3, created_at: 4.days.ago) } let!(:shipment_4) { create(:shipment, product: product_4, created_at: 5.days.ago) }
  23. let(:output_records) do [ [shipment_4.created_on, shipment_4.completed_on, user.name, product_4.name], [shipment_3.created_on, shipment_3.completed_on, user.name,

    product_3.name], [shipment_2.created_on, shipment_2.completed_on, user.name, product_2.name], [shipment_1.created_on, shipment_1.completed_on, user.name, product_1.name], ] end
  24. describe "ordering" do it "reorders shipments by their creation date"

    do old = create_shipment(date: 9.days.ago) new = create_shipment(date: 2.days.ago) report = described_class.new(user, [new, old]) result = report.data expect(result.map(&:first)).to eq( [old.created_on, new.created_on] ) end end
  25. # let(:product_1) { create(:product) } # let(:product_2) { create(:product) }

    # let(:product_3) { create(:product) } # let(:product_4) { create(:product) } # let!(:shipment_1) { create(:shipment, product: product_1, # created_at: 2.days.ago) } # let!(:shipment_2) { create(:shipment, product: product_2, # created_at: 3.days.ago) } # let!(:shipment_3) { create(:shipment, product: product_3, # created_at: 4.days.ago) } # let!(:shipment_4) { create(:shipment, product: product_4, # created_at: 5.days.ago) } # # let(:shipments) do # [ # shipment_1, # shipment_2, # shipment_3, # shipment_4 # ] # end # let(:output_records) do # [ # [shipment_4.created_on, shipment_4.completed_on, # user.name, product_4.name], # [shipment_3.created_on, shipment_3.completed_on, # user.name, product_3.name], # [shipment_2.created_on, shipment_2.completed_on, # user.name, product_2.name], # [shipment_1.created_on, shipment_1.completed_on, # user.name, product_1.name], # ] # end
  26. describe "ordering" do it "reorders shipments by their creation date"

    do old = create_shipment(date: 9.days.ago) new = create_shipment(date: 2.days.ago) report = described_class.new(user, [new, old]) result = report.data expect(result.map(&:first)).to eq( [old.created_on, new.created_on] ) end end
  27. describe "ordering" do it "reorders shipments by their creation date"

    do # Given old = create_shipment(date: 9.days.ago) new = create_shipment(date: 2.days.ago) # When report = described_class.new(user, [new, old]) result = report.data # Then expect(result.map(&:first)).to eq( [old.created_on, new.created_on] ) end end
  28. it "reorders shipments by their creation date" do old =

    create_shipment(date: 9.days.ago) new = create_shipment(date: 2.days.ago) report = described_class.new(user, [new, old]) expect(report.data.map(&:first)).to eq([old, new]) end
  29. it "returns order units summed, divided by units per shipper"

    do create_store_order(count_units: 4) create_store_order(count_units: 8) subject = described_class.new(store_orders: store_orders) total_shippers = subject.total_shipper_count expect(total_shippers).to eq(3) end
  30. it "returns a ceiling rounded value" do create_store_order(count_units: 7) subject

    = described_class.new(store_orders: store_orders) total_shippers = subject.total_shipper_count expect(total_shippers).to eq(2) end
  31. let!(:shipment_1) { create(:shipment, product: product_1, created_at: 2.days.ago) } let!(:shipment_2) {

    create(:shipment, product: product_2, created_at: 3.days.ago) } let!(:shipment_3) { create(:shipment, product: product_3, created_at: 4.days.ago) } let!(:shipment_4) { create(:shipment, product: product_4, created_at: 5.days.ago) }
  32. it "returns order units summed, divided by units per shipper"

    do create_store_order(count_units: 4) create_store_order(count_units: 8) subject = described_class.new(store_orders: store_orders) total_shippers = subject.total_shipper_count expect(total_shippers).to eq(3) end it "returns a ceiling rounded value" do create_store_order(count_units: 7) subject = described_class.new(store_orders: store_orders) total_shippers = subject.total_shipper_count expect(total_shippers).to eq(2) end
  33. describe "#total_shipper_count" do subject { described_class.new(store_orders: StoreOrder.all) } context "even

    division" do let!(:order1) { create_store_order(count_units: 4) } let!(:order2) { create_store_order(count_units: 8) } let!(:expected_total) { 3 } # ... lots of other tests ... it "returns order units summed, divided by units per shipper" do expect(subject.total_shipper_count).to eq(expected_total) end end end
  34. context "rounding" do let!(:order) { create_store_order(count_units: 7) } let!(:expected_total) {

    2 } # ... lots of other tests ... it "returns order units summed, divided by units per shipper" do expect(subject.total_shipper_count).to eq(expected_total) end end
  35. it "returns a user summary for printing" do user =

    User.new(id: 5, full_name: "Dave G", role: :admin) expected = "(#{user.id}) #{user.full_name}, #{user.role}" summary = user_summary(user) expect(summary).to eq(expected) end
  36. it "returns a user summary for printing" do user =

    User.new(id: 5, full_name: "Dave G", role: :admin) summary = user_summary(user) expect(summary).to eq("(5) Dave G, admin") end
  37. describe "#process" do it “has a correct return value" end

    describe "#process" do it "returns the number of records correctly persisted" end
  38. it “picks randomly, but correctly” do double1 = double double2

    = double result = [double1, double2].sample expect(result).to eq(double1) end # expected: #<Double (anonymous)> # got: #<Double (anonymous)>
  39. it “picks randomly, but correctly” do double1 = double("first record")

    double2 = double("second record”) result = [double1, double2].sample expect(result).to eq(double1) end # expected: #<Double "first record"> # got: #<Double "second record">
  40. it "checks result size" do arr = [1,2,3] response =

    arr.length expect(response > 3).to be_true end # expected true # got false
  41. it "checks result size" do arr = [1,2,3] response =

    arr.length expect(response).to be > 3 end # expected: > 3 # got: 3
  42. it "checks validity of a record" do thing = double(valid?:

    true) expect(thing.valid?).to be_false end # expected: false # got: true
  43. it "checks validity of a record" do thing = double("order",

    valid?: true) expect(thing).not_to be_valid end # expected `#<Double "order">.valid?` to return false, got true
  44. class JobLog < ActiveRecord::Base validate :failure_message, presence: true, if: :failed?

    end it "requires failure_message for failures” do job = JobLog.new(status: :failed) expect(job).to validate_presence_of(:failure_message) job = JobLog.new(status: :completed) expect(job).not_to validate_presence_of(:failure_message) end
  45. def user_summary(user) "(#{user.id}) #{user.full_name}, #{user.role}" end it "returns a user

    summary for printing, bad" do user = double(id: 5, full_name: "Dave G", role: :admin) response = user_summary(user) expect(response).to eq("(5) Dave G, admin") end
  46. it "returns a user summary for printing, better" do user

    = instance_double(User, id: 5, full_name: "Dave G", role: :admin) response = user_summary(user) expect(response).to eq("(5) Dave G, admin") end
  47. it "returns a user summary for printing, better" do user

    = User.new(id: 5, full_name: "Dave G", role: :admin) expect(user_summary(user)).to eq("(5) Dave G, admin") end
堤防是什么意思 性病是什么症状 autumn是什么意思 肠梗阻是什么意思 血红蛋白是什么意思
呲牙咧嘴是什么意思 亚麻跌是什么意思 猫什么时候发情 女人脱发是什么原因 红薯什么时候掐尖
内能与什么有关 唐顿庄园讲的是什么 什么叫人彘 npv是什么病毒 叶酸什么时间吃最好
采耳是什么意思 血压低是什么情况 宝宝在肚子里打嗝是什么原因 举牌什么意思 九二共识是什么意思
四季平安是什么生肖0297y7.com 瘢痕是什么hcv8jop6ns6r.cn 茶歇是什么意思jinxinzhichuang.com catl是什么意思hcv8jop5ns6r.cn 脱发补充什么维生素hcv7jop9ns9r.cn
传宗接代是什么意思mmeoe.com 眼睛发炎用什么药效果好hcv8jop3ns4r.cn 舌头发麻是什么情况hcv8jop2ns6r.cn 好奇害死猫是什么意思hcv8jop9ns4r.cn 过期红酒有什么用途hcv8jop6ns9r.cn
里脊肉是什么肉hcv7jop5ns0r.cn 糖化血红蛋白是什么意思hcv9jop7ns1r.cn 影像是什么意思hcv7jop5ns6r.cn 肚子拉稀是什么原因mmeoe.com 喝蜂蜜水有什么好处hcv9jop1ns7r.cn
手足口病是什么jiuxinfghf.com 一天吃一个苹果有什么好处xscnpatent.com 床上出现蜈蚣什么原因hcv7jop4ns5r.cn 什么私语zhongyiyatai.com 喝碱性水有什么好处hcv9jop3ns3r.cn
百度