How can I test :inclusion validation in Rails using RSpec

Use shoulda_matchers In recent versions of shoulda-matchers (at least as of v2.7.0), you can do: expect(subject).to validate_inclusion_of(:active).in_array(%w[Y N]) This tests that the array of acceptable values in the validation exactly matches this spec. In earlier versions, >= v1.4 , shoulda_matchers supports this syntax: it {should ensure_inclusion_of(:active).in_array(%w[Y N]) }

Shoulda/RSpec matchers – conditional validation

It doesn’t appear that shoulda_matchers does this, but it’s easy enough to write it yourself:: context “if eligible” do before { allow(subject).to receive(:eligible?).and_return(true) } it { should validate_presence_of(:name) } end context “if ineligible” do before { allow(subject).to receive(:eligible?).and_return(false) } it { should_not validate_presence_of(:name) } end

How to run Rails console in the test environment and load test_helper.rb?

For Rails < 3.0 Run script/console –help. You’ll notice that the syntax is script/console [environment], which in your case is script/console test. I’m not sure if you have to require the test helper or if the test environment does that for you, but with that command you should at least be able to boot successfully … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)