Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test signup validation in rspec? (rails tutorial chapter 7 exercises)
    text
    copied!<p>I think this is as much a programming practice question as it is a technical question.</p> <p>I am doing the Ruby on Rails Tutorial and am up to the <a href="http://ruby.railstutorial.org/chapters/sign-up#sec-signup_exercises" rel="nofollow">Chapter 7 Exercises</a>, question 2. This question asks you to write tests that verify the error messages for an invalid user appear on the reloaded signup page (eg blank password, invalid email address).</p> <p>I have some code that works fine enough but I don't feel I'm actually testing anything. I have simply tested for the presence of the error messages that appeared when I manually entered a blank user into the signup page. Unsurprisingly, the tests pass - I simply copied and pasted the error messages into the tests!</p> <p>The relevant part of my <code>user_pages_spec.rb</code> are:</p> <pre><code>describe "signup" do before { visit signup_path } let(:submit) { "Create my account" } describe "with invalid information" do it "should not create a user" do expect { click_button submit }.not_to change(User, :count) end describe "after submission" do before { click_button submit } it { should have_selector('title',text: 'Sign up') } it { should have_content('error') } it { should have_content('Password digest can\'t be blank') } it { should have_content('Name can\'t be blank') } it { should have_content('Email can\'t be blank') } it { should have_content('Email is invalid') } it { should have_content('Password can\'t be blank') } it { should have_content('Password is too short (minimum is 6 characters)') } it { should have_content('Password confirmation can\'t be blank') } end end </code></pre> <p><strong>Is this the right way to do it? Shouldn't I be getting Ruby to tell me what the error messages / translations are, and testing those? What happens if I change one of my validation conditions, like password length?</strong> Similarly, when I fix the error message about Password Digest, should I have to change my testing file?</p> <p>Edit: <strong>What if I don't know what the exact error string will be?</strong> Shouldn't Ruby tell the test what the error message will be? <strong>Should I be using 'have(x).errors_on' for this?</strong>. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload