Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails rspec模型验证与_options语句_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails rails rspec模型验证与_options语句

Ruby on rails rails rspec模型验证与_options语句,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我使用rspec来测试我的模型。我的模型验证中有with_options语句,我如何在测试中处理它 with_options({on: :some}) do |s| s.validates :name, presence: true end with_options({on: :some2}) do |s2| s2.validates :name, presence: true end 这非常简单,当您想要验证具有with_选项的模型时,它与其他部分一样平常 @myinstance.wh

我使用rspec来测试我的模型。我的模型验证中有with_options语句,我如何在测试中处理它

with_options({on: :some}) do |s|
 s.validates :name, presence: true
end

with_options({on: :some2}) do |s2|
 s2.validates :name, presence: true
end

这非常简单,当您想要验证具有with_选项的模型时,它与其他部分一样平常


@myinstance.when不应该是有效的(:some)

当您想要验证具有with选项的模型时,它非常简单,并且与其他部分一样


@myinstance.should\u无效(:部分)

thos
with\u options
只是一个简单的选项,但您仍然可以测试模型验证。带有_选项的不应该是问题。你能展示你为此编写的测试代码吗?它是这样的:`it'有效,有效名称为“do@address.should\u valid end it”无效,没有名称为“do@address.name=nil@address.should\u not be\u valid end`一般答案是“您编写了一个测试,以确保您希望发生的事情实际上正在发生,而您不希望发生的事情不会发生。“一个更具体的答案是我通常使用的shoulda matchers gem.thos
with_options
只是一个简单的例子,但是你仍然可以测试模型验证。带有_选项的不应该是问题。你能展示你为此编写的测试代码吗?它是这样的:`it'有效,有效名称为“do@address.should\u valid end it”无效,没有名称为“do@address.name=nil@address.should\u not be\u valid end`一般答案是“你编写一个测试,确保你想要发生的事情实际上正在发生,而你不想要的事情不会发生。”一个更具体的答案是我通常使用的shoulda matchers gem。