do it "name: nil is expected not to be valid" do user = build(:user, name: nil) expect(user).to_not be_valid end end describe "methods" do ... end end ※ 補⾜ describeは「説明する」、expectは「期待する」という意味です。 また、it =example には単⼀のテストを記述します。
"active is expected to be false" do user = build(:user) expect(user.active?).to be false end end context "after confirm" do it "active is expected to be true" do user = build(:user, :confirmed) expect(user.active?).to be true end end end
= build(:user) end let(:user) { build(:user) } describe "validations" do it "name: nil is expected not to be valid" do expect(user).to_not be_valid end end end beforeと違う点は、letは遅延評価されるという点です。
"before confirm" do let(:user) { build(:user) } it { is_expected.to be false } end context "after confirm" do let(:user) { build(:user, :confirmed) } it { is_expected.to be true } end end
user } it { is_expected.to belong_to(:company) } it { is_expected.to have_many(:restaurants).through(:company) } it { is_expected.to have_secure_password } it { is_expected.to_not allow_value(nil).for(:name) } it { is_expected.to_not allow_value(nil).for(:password) } it { is_expected.to_not allow_value(nil).for(:mail_address) } it { is_expected.to validate_length_of(:zipcode1).is_at_most(3) } it { is_expected.to validate_length_of(:zipcode2).is_at_most(4) } it { is_expected.to validate_numericality_of(:zipcode1).only_integer } it { is_expected.to validate_numericality_of(:zipcode2).only_integer } end