Search code examples
ruby-on-railsrspeccapybara

How to test that a form successfully submitted using Rspec?


I'm writing feature specs for my Article form.

In one test, I have RSpec look for certain form inputs using the labels:

expect(find_field("Title")).to_not be_nil

Now I'd like to know if the form was submitted properly. How can I do this using RSpec, if you aren't supposed to check the database from a feature spec?

For example, what if I mistyped the name attribute on the Title input? The label would still be found by my find_field() call, and controller specs would have me specify the title in a hash.


Solution

  • Nine years later, I'm still doing Rails, and I've since gained enough experience to answer my own question.

    Just check the database and don't worry about "what you're supposed to do".

    Typically, what I've done is:

    it "does what it is supposed to" do
      article = create(:article)
      visit edit_article_path(article)
      
      fill_in "Title", with: "Whatever" 
    
      expect(page).to have_content("Your success message")
      article.reload
      expect(article.title).to eq "Whatever"
    end