Ruby 测试产品的成分和属性[Ruby on Rails]

13 篇文章 0 订阅

Ruby 测试产品的成分和属性[Ruby on Rails]

Ruby Test 通常会涵盖大部分的功能与链接,在这里主要着重介绍的是如何测试目标产品
对于产品测试,首先最先测试的应该是该产品基础信息(组成部分):
比如这里关于产品细节的测试,标号,价钱描述等等都被包含了.

# == Schema Information
#
# Table name: products
#
#  id          :bigint           not null, primary key
#  cost        :float
#  description :text
#  name        :string
#  stock_level :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  category_id :bigint
#
# Indexes
#
#  index_products_on_category_id  (category_id)
#
# Foreign Keys
#
#  fk_rails_...  (category_id => categories.id)
#

require 'rails_helper'

describe Product do

  describe '#value_of_stock' do

    it 'calculates the total value of the stock' do
      product = Product.new(cost: 10, stock_level: 300)
      expect(product.value_of_stock).to eq 3000
    end

  end

  describe '#price_with_vat' do

    it 'calculates the price with VAT at 20%' do

    end

    it 'calculates the price with VAT to be 0 when the cost is 0' do

    end

  end

  describe '#shipping_and_handling' do

    it 'calculates the shipping cost + the set percentage' do
      product = Product.new(cost: 10, stock_level: 300)
      mock_category = double('category', shipping_cost: 10, handling_percentage: 5)
      allow(product).to receive(:category).and_return(mock_category)
      expect(product.shipping_and_handling).to eq 10.5
    end

    it 'accepts 0 shipping cost' do

    end

    it 'accepts 0 handling percentage' do

    end

    it 'returns nil if there is no category' do

    end

  end

end

上面的代码是我们使用Rspec创建的单元测试的一个示例。其中的一些关键要素包括:
● 需要“rails_helper”——默认情况下,测试文件独立运行。我们需要这一行来加载Rails框架和所有应用程序代码。
● 描述产品–这标识了我们正在测试的类别。在这种情况下,它是产品模型。● 描述“股票价值”——这是我们正在测试的方法。#是一种约定,用于表示它是一个实例方法。如果我们想测试一个类方法(即def self.some_方法),那么我们会编写一个“.”取而代之的是(例如,描述“.some_method”)。
在这种情况下,如果您销售了所有产品,则“库存价值”方法旨在计算总销售价值。这是通过将库存水平乘以产品成本来实现的。
● 它“计算股票的总价值”——这是一个测试案例。
在这里,我们将为这种情况编写测试代码。● 产品=产品。新建(成本:10,库存水平:300)——这将创建一个新的产品实例,成本为10,库存水平为300。大多数测试都需要这样的设置。你需要知道你正在测试的对象的状态,才能知道你期望的结果。● 预期(产品、股票价值)。对于等式3000,这是一个断言。断言是一个可以是真或假的表达式。
这是您表示测试用例期望的方式。如果满足预期,则测试用例将通过;否则它将失败。
这个测试用例需要一个产品。股票的价值等于3000。它将创建一个新的Product实例,然后调用Product。股票的价值。如果方法返回3000,测试用例将通过;如果它返回任何其他内容,它将失败。

写完这个test文件后,在终端输入来运行:

bundle exec rspec spec/models/product_spec.rb

spec/models/product_spec.rb是具体的路径和文件名, 运行后如果测试文件本身没有问题, 应该会显示出Coverge来表明你已经测试了整个系统的百分之几

第一个案例测试了产品需要按标准税率增加增值税的典型场景。
第二种情况是测试产品成本为0的边缘情况。边缘情况是指参数位于预期边界的最末端或需要以某种方式进行特殊处理的情况。完成两个空测试用例并检查它们是否通过。您还可以尝试在断言中输入错误的值,以便在断言失败时查看输出。模拟和存根如前所述,单元测试用于与其他代码隔离地测试代码。由于上面的方法没有直接的依赖关系,因此没有直接的依赖关系。然而,在某些情况下,被测试的方法将与其他类交互。模拟和存根是两种技术,可以用来确保单元测试总是孤立地测试代码。

# spec/string_calculator_spec.rb
describe StringCalculator do

  describe ".add" do
    context "given '4'" do
      it "returns 4" do
        expect(StringCalculator.add("4")).to eql(4)
      end
    end

    context "given '10'" do
      it "returns 10" do
        expect(StringCalculator.add("10")).to eql(10)
      end
    end
  end
end

上述测试是关于输入单个数字,得到结果

require “calculator”
describe Calculator do
     context “Given two numbers” do
          it “can add the numbers using the add method” do
               calc = Calculator.new
               x = 13
               y = 5
               expect(calc.add(x, y)).to eq(18)
          end
     end
end

在这里插入图片描述

谢谢阅读,如果觉得帮助到你了的话麻烦点个赞.
同时如果有什么关于ruby的问题也可以私信我.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值