简体   繁体   中英

How do I write this spec in MiniTest/shoulda syntax?

I have this spec that I want to translate to MiniTest.

describe User do
  subject { build(:user, provider: 'foo') }

  # don't validate presence of password when provider is present
  it do
    should_not validate_presence_of(:password)
  end
end

I tried this. I am getting an error of undefined method 'should_not' for UserTest

class UserTest < ActiveSupport::TestCase
  def setup
    @user = build_stubbed(:user)
  end

  test "responds to name" do
    assert_respond_to @user, :name
  end

  should validate_presence_of(:password)

  test "do not validate presence of password when provider is present" do
    build_stubbed(:user, provider: 'foo')
    should_not validate_presence_of(:password)
  end
end

I want to change the context for one test, where the subject gets a provider attribute, which should disable the presence validator on the password field.

Here's the full error:

UserTest#test_presence_of_password:
NoMethodError: undefined method `should_not' for #<UserTest:0x007feaa82c1c68>
    test/models/user_test.rb:45:in `block in <class:UserTest>'

I found that the better way to do this is to revert to good old MiniTest:

  test "uniqueness of email with a different provider" do
    email_user = create(:user, email: "foo@bar.com")
    facebook_user = build_stubbed(:facebook_user, email: "foo@bar.com")

    assert facebook_user.valid?, "should be valid with same email if provider is different"
  end

Take a look at the minitest-rails-shoulda gem. If you use it I assume the test would look like this:

describe User do
  subject { build_stubbed(:user) }

  it { must validate_presence_of(:password) }

  describe "when a provider is present" do
    subject { build_stubbed(:user, provider: 'foo') }

    it { wont validate_presence_of(:password) }      
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM