rspec应该ensure_length_of allow_nil

Ole*_*huk 1 rspec ruby-on-rails shoulda

模型

validates_length_of :description, :maximum => 255, :allow_nil => true
Run Code Online (Sandbox Code Playgroud)

spec_file

it { should ensure_length_of(:description).is_at_most(255).allow_nil }
Run Code Online (Sandbox Code Playgroud)

回归

Failure/Error: it { should ensure_length_of(:description).is_at_most(255).allow_nil }
 NoMethodError:
   undefined method `allow_nil' for #<Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher:0x0000000acb03e0>
Run Code Online (Sandbox Code Playgroud)

请帮忙!

xda*_*azz 7

没有allow_nil办法Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher.

你可以使用allow_value:

it { should allow_value(nil).for(:description) }
it { should ensure_length_of(:description).is_at_most(255) }
Run Code Online (Sandbox Code Playgroud)