2016年9月16日金曜日

「Phoenix」フレームワークで、モデルのテスト

Phoenix」フレームワークで、モデルのテストです。ドキュメントを参考に動かしてみました。

/test/models/user_test.exsuser_test.exs
defmodule HelloPhoenix.UserTest do
  use HelloPhoenix.ModelCase

  alias HelloPhoenix.User

  @valid_attrs %{bio: "some content some content some content", email: "some@content.com", name: "some content", number_of_pets: 42}
  @invalid_attrs %{}

  test "changeset with invalid attributes" do
    changeset = User.changeset(%User{}, @invalid_attrs)
    refute changeset.valid?
  end
  
  test "changeset with valid attributes" do
    changeset = User.changeset(%User{}, @valid_attrs)
    assert changeset.valid?
  end
  
  test "bio is not required" do
    changeset = User.changeset(%User{}, Map.delete(@valid_attrs, :bio))
    refute changeset.valid?
  end
  
  test "email is not required" do
    changeset = User.changeset(%User{}, Map.delete(@valid_attrs, :email))
    refute changeset.valid?
  end
  
  test "name is not required" do
    changeset = User.changeset(%User{}, Map.delete(@valid_attrs, :name))
    refute changeset.valid?
  end
  
  test "number_of_pets is not required" do
    changeset = User.changeset(%User{}, Map.delete(@valid_attrs, :number_of_pets))
    refute changeset.valid?
  end
  
  test "number_of_pets is less than 999" do
    attrs = %{@valid_attrs | number_of_pets: 9999}
    changeset = User.changeset(%User{}, attrs)
    refute changeset.valid?
  end
  
  test "number_of_pets is more than 0" do
    attrs = %{@valid_attrs | number_of_pets: -9}
    changeset = User.changeset(%User{}, attrs)
    refute changeset.valid?
  end
  
  test "bio must be at least 10 characters long" do
    attrs = %{@valid_attrs | bio: "content"}
    changeset = User.changeset(%User{}, attrs)
    refute changeset.valid?
  end
  
  test "email must contain at least an @" do
    attrs = %{@valid_attrs | email: "fooexample.com"}
    assert {:email, "無効な形式が含まれています"} in errors_on(%User{}, attrs)
  end
end

参考サイト
Models(Phoenix)

0 件のコメント:

コメントを投稿