Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 如何为空白字段编写rspec?[铁路3.1]_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 如何为空白字段编写rspec?[铁路3.1]

Ruby on rails 如何为空白字段编写rspec?[铁路3.1],ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我使用rails 3.1+rspec和factory girl 我对必填字段的验证(验证是否存在)正在工作。 如何让测试将该事实作为“成功”而不是“失败” 规格为: describe "Add an industry with no name" do context "Unable to create a record when the name is blank" do subject do ind = Factory.create(:industry_name_bla

我使用rails 3.1+rspec和factory girl

我对必填字段的验证(验证是否存在)正在工作。 如何让测试将该事实作为“成功”而不是“失败”
规格为:

describe "Add an industry with no name" do
  context "Unable to create a record when the name is blank" do
    subject do
      ind = Factory.create(:industry_name_blank)
    end
    it { should be_invalid }
  end
end
但我失败了:

Failures:

  1) Add an industry with no name Unable to create a record when the name is blank 
     Failure/Error: ind = Factory.create(:industry_name_blank)
     ActiveRecord::RecordInvalid:
       Validation failed: Name can't be blank
     # ./spec/models/industry_spec.rb:45:in `block (3 levels) in <top (required)>'
     # ./spec/models/industry_spec.rb:47:in `block (3 levels) in <top (required)>'

Finished in 0.20855 seconds
8 examples, 1 failure

这里有一个例子。。。按照惯例,主题将填充“Industry.new”

describe Industry do

  it "should have an error on name when blank" do
    subject.name.should be_blank
    subject.valid?
    subject.should have(1).error_on(:name)
    #subject.errors.on(:name).should == "is required"
  end

end
最后一个比较脆弱,但你可以做到

有关语法的详细信息:

Factory.build(:industry\u name\u blank)
生成对象,而
Factory.create(:industry\u name\u blank)
生成并保存创建的对象。在您的情况下,它无法保存对象,因为由于缺少
名称
,该对象无效,这就是您得到验证错误的原因

因此,不要使用
create
而是使用
build
来避免出现验证错误:
Factory.build(:industry\u name\u blank)
。然后你应该能够像Jesse建议的那样详细说明:

subject.should_not be_valid
subject.should have(1).error_on(:name)

+1的博客链接。。。我一直在寻找类似的东西不,-Failures:`1)添加一个没有名称的行业在空白时应该有一个名称错误失败/错误:subject.name.should_blank NoMethodError:undefined method
name'“添加一个没有名称的行业”:String 35;/spec/models/industry_spec.rb:44:in
block(2级)在“在0.19527秒内完成8个示例,1个失败”中,正如您可以看到的那样,虽然
name
属性确实存在,但我也将+1用于bloglink!我试过:描述“添加一个空白行业”在(:each)do@industry=Factory(:industry\u name\u blank)结束之前做什么“空白时名称上应该有错误”do@industry.should\u无效@industry.name.valid@industry.should有(1).error_on(:name)#@industry.errors.on(:name).should==“is required”结束但我得到
失败:1)添加一个没有名称的行业在名称为空时应该有错误失败/错误:@industry=Factory(:industry_name_blank)ActiveRecord::RecordInvalid:验证失败:名称不能为空
嘿,这是对这两个问题的一个很好的解释+1谢谢雅各布:)我把你的答案编辑了一下。请检查它是否仍然适合您。
describe Industry do

  it "should have an error on name when blank" do
    subject.name.should be_blank
    subject.valid?
    subject.should have(1).error_on(:name)
    #subject.errors.on(:name).should == "is required"
  end

end
subject.should_not be_valid
subject.should have(1).error_on(:name)