Rails和Rspec - 在表单提交失败时检查是否存在错误

ste*_*och 6 rspec ruby-on-rails rspec2 ruby-on-rails-3

我有一个Foo模型,其中:创建时需要名称.

我正在编写一个规范来测试验证

it 'should not create an invalid Foo' do
  fill_in "Name", :with=>""
  # an error message will be displayed when this button is clicked
  click_button "Create Foo"
end
Run Code Online (Sandbox Code Playgroud)

如何确认页面上是否存在错误消息?

我试过page.errors.should have_key(:name)但这不对.

我想我可以做page.should have_content("Name can't be blank")但我宁愿避免将我的集成测试与内容强烈耦合

mhr*_*ess 12

如果您在单元测试级别正确测试验证,则可以轻松地为所需的错误消息添加另一个测试:

describe Foo do
  describe "validations" do
    describe "name" do
      before { @foo = FactoryGirl.build(:foo) } # or Foo.new if you aren't using FactoryGirl

      context "when blank" do
        before { @foo.name = "" }

        it "is invalid" do
          @foo.should_not be_valid
        end

        it "adds the correct error message" do
          @foo.valid?
          @foo.errors.messages[:name].should include("Name cannot be blank")
        end
      end # positive test case omitted for brevity
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这样,您已经隔离了错误生成并复制到模型,并且可靠地进行了测试,这允许您实现某种全局错误显示(例如,使用flash [:error],而无需测试每个错误消息明确地在视图级别.


Jes*_*ott 6

你说你正在编写一个规范来测试验证,但我看到你用"fill_in"测试了水豚(或类似)

相反,我强烈建议编写单元测试来测试您的模型.

规格/型号/ your_model_spec.rb

require 'spec_helper'
describe YourModel do

  it "should not allow a blank name" do
    subject.name = ""
    subject.should_not be_valid
    subject.should have(1).error_on(:name)
  end

end
Run Code Online (Sandbox Code Playgroud)

这样,你就是在隔离测试 - 只测试你需要测试的内容,而不是控制器是否正常工作,或视图,甚至是通过闪存循环.

这样,您的测试快速,耐用且隔离.