我有这个规范,我想翻译成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
我试过这个。我收到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
我想更改一个测试的上下文,其中主题获得provider
属性,该属性应该禁用密码字段上的状态验证器。
这是完整的错误:
UserTest#test_presence_of_password:
NoMethodError: undefined method `should_not' for #<UserTest:0x007feaa82c1c68>
test/models/user_test.rb:45:in `block in <class:UserTest>'
答案 0 :(得分:0)
我发现更好的方法是恢复旧的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
答案 1 :(得分:0)
看看minitest-rails-shoulda宝石。如果你使用它,我认为测试看起来像这样:
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