[英]How do I write this spec in MiniTest/shoulda syntax?
I have this spec that I want to translate to MiniTest. 我有这个要转换为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
我收到
undefined method 'should_not' for UserTest
的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. 我想更改一个测试的上下文,在该测试中,主题获取
provider
属性,该属性应禁用密码字段上的状态验证器。
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: 我发现执行此操作的更好方法是还原为旧的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. 看看minitest-rails-shoulda宝石。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.