1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

テストコードの可読性を上げるために

Posted at

商品出品機能の単体テストコードを記述してレビューをいただいた。
どうやらcontextを使うと同じ内容でも可読性が高くなるとのこと。

こちらがcontextを使う前のテストコードの記述

require "rails_helper"

RSpec.describe Item, type: :model do
  before do
    @item = FactoryBot.build(:item)
  end
  describe "出品" do
    it "全ての情報が正しく入力されていれば出品できる" do
      expect(@item).to be_valid
    end
    it "imageが空だと出品できない" do
      @item.image = nil
      @item.valid?
      expect(@item.errors.full_messages).to include("Image can't be blank")
    end
    it "prduct_nameが空だと出品できない" do
      @item.product_name = ""
      @item.valid?
      expect(@item.errors.full_messages).to include("Product name can't be blank")
    end
    it "product_descriptionが空だと出品できない" do
      @item.product_descriiption = ""
      @item.valid?
      expect(@item.errors.full_messages).to include("Product descriiption can't be blank")
    end
    it "category_idが1以外でないと出品できない" do
      @item.category_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Category must be other than 1")
    end
    it "product_condition_idが1以外でないと出品できない" do
      @item.product_condition_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Product condition must be other than 1")
    end
    it "shipping_charge_idが1以外でないと出品できない" do
      @item.shipping_charge_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Shipping charge must be other than 1")
    end
    it "delivery_area_idが1以外でないと出品できない" do
      @item.delivery_area_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Delivery area must be other than 1")
    end
    it "days_to_delivery_idが1以外でないと出品できない" do
      @item.days_to_delivery_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Days to delivery must be other than 1")
    end
    it "priceが空だと出品できない" do
      @item.price = ""
      @item.valid?
      expect(@item.errors.full_messages).to include("Price can't be blank")
    end
    it "priceが300未満だと出品できない" do
      @item.price = 299
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "priceが10000000以上だと出品できない" do
      @item.price = 10_000_000
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "priceが全角数字では出品できない" do
      @item.price = "10000"
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "priceが半角英字では出品できない" do
      @item.price = "sample"
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "priceが半角英数字混合では出品できない" do
      @item.price = "sample1000"
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "userが紐付いていないと出品できない" do
      @item.user = nil
      @item.valid?
      expect(@item.errors.full_messages).to include("User must exist")
    end
  end
end

contextを使うと以下のようなテストコードに

require "rails_helper"

RSpec.describe Item, type: :model do
  before do
    @item = FactoryBot.build(:item)
  end
  describe "出品" do
    context "全ての情報が正しく入力されていれば" do
      it "出品できる" do
        expect(@item).to be_valid
      end
    end
    context "imageが空だと" do
      it "出品できない" do
        @item.image = nil
        @item.valid?
        expect(@item.errors.full_messages).to include("Image can't be blank")
      end
    end
    it "prduct_nameが空だと出品できない" do
      @item.product_name = ""
      @item.valid?
      expect(@item.errors.full_messages).to include("Product name can't be blank")
    end
    it "product_descriptionが空だと出品できない" do
      @item.product_descriiption = ""
      @item.valid?
      expect(@item.errors.full_messages).to include("Product descriiption can't be blank")
    end
    it "category_idが1以外でないと出品できない" do
      @item.category_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Category must be other than 1")
    end
    it "product_condition_idが1以外でないと出品できない" do
      @item.product_condition_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Product condition must be other than 1")
    end
    it "shipping_charge_idが1以外でないと出品できない" do
      @item.shipping_charge_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Shipping charge must be other than 1")
    end
    it "delivery_area_idが1以外でないと出品できない" do
      @item.delivery_area_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Delivery area must be other than 1")
    end
    it "days_to_delivery_idが1以外でないと出品できない" do
      @item.days_to_delivery_id = 1
      @item.valid?
      expect(@item.errors.full_messages).to include("Days to delivery must be other than 1")
    end
    it "priceが空だと出品できない" do
      @item.price = ""
      @item.valid?
      expect(@item.errors.full_messages).to include("Price can't be blank")
    end
    it "priceが300未満だと出品できない" do
      @item.price = 299
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "priceが10000000以上だと出品できない" do
      @item.price = 10_000_000
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "priceが全角数字では出品できない" do
      @item.price = "10000"
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "priceが半角英字では出品できない" do
      @item.price = "sample"
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "priceが半角英数字混合では出品できない" do
      @item.price = "sample1000"
      @item.valid?
      expect(@item.errors.full_messages).to include("Price is not included in the list")
    end
    it "userが紐付いていないと出品できない" do
      @item.user = nil
      @item.valid?
      expect(@item.errors.full_messages).to include("User must exist")
    end
  end
end

最初はカラムごとに細分化して記述しないといけないと思っていたが
できる、できないの大分類で分けるだけでも可読性が高くなるとのこと。
ただカラムごとで分けて記述する方がより可読性が高くなるとのこと。
例えば初めにpriceのカラムのテストコードを記述して
他のカラムを挟んでまた急にpriceのカラムのテストコードが記述してあると
可読性が低くなってしまう。

実際にエンジニアでチームで働く上で同じチームの方が見やすい方が生産性が上がるので
今後も可読性が少しでも高く正常に動くコードの記述を心がけたい。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?