Chris
Chris

Reputation: 86

testing flash[:notice] with capybara and activerecord NameError: undefined local variable or method `flash'

I am very new to building websites and am currently running into the following problem.

I am using ruby, rspec, sinatra, sinatra-flash, capybara and ActiveRecord to design a website.

I am not using rails currently

I have implemented a user signup/login page whereby if the user does not fill up all the fields on sign-in a flash[:notice] should pop up. When I feature test this myself by using sinatra's rackup, it works perfectly. However, when I run rspec using capybara it raises the error:

NameError:
       undefined local variable or method `flash'

I cannot figure out why this would be. All my other tests pass.

My feature test code:

scenario "not filling a field, raises message 'Please ensure you have filled all the fields'" do
    visit('/')
    click_button("Sign up")
    fill_in 'username', with: 'ChrisL'
    fill_in 'last_name', with: 'Lovell'
    fill_in 'first_name', with: 'Chris'
    fill_in 'email', with: '[email protected]'

    expect(current_path).to eq '/sign-up'
    expect(page).to_not have_content "Welcome, ChrisL"
    expect(flash[:notice]).to eq "Please ensure you have filled all the fields"
  end

My controller code(removed other routes):

class Chitter < Sinatra::Base
    
enable :sessions
register Sinatra::Flash

get '/sign-up' do
    erb :sign_up
end

post '/signing-up' do
  @new_user = User.create(
    username: params[:username], 
    email: params[:email],
    first_name: params[:first_name],
    last_name: params[:last_name],
    password: params[:password]
  )
  @new_user.save
        
  if User.find_by_id(@new_user.id)
    session[:user_id] = @new_user.id
    redirect '/'
  else 
    flash[:notice] = 'Please ensure you have filled all the fields'
    redirect '/sign-up'
  end
end
end

My erb file:

<!DOCTYPE html>
<html>
  <head>
    <title> Chitter Sign In/ Sign Up </title>
  </head>
  <body>
    <header>
      <h1>Chitter: <small>Sign In</small></h1>
    </header>
    <h2 style="color:blue">
      <%= flash[:notice] %>
    </h2>
    <form action="/logging-in" method="post">
      <label for="username">
        Enter Username
        <input type="text" name="log_in_username">
      </label>
      <label for="password">
        Enter A Password 
        <input type="password" name="log_in_password">
      </label>
      <input type="submit" value="Log in">
    </form>
      <br>
    <header>
      <h1>Chitter: <small>Sign Up</small></h1>
    </header>
    <h3> Sign Up For The Unique Experience </h3>
    <form action="/signing-up" method="post">
      <label for="username">
        Enter Desired Username
        <input type="text" name="username">
      </label>
      <br>
      <label for="first_name">
        Enter Your First Name
        <input type="text" name="first_name">
      </label>
      <br>
      <label for="last_name">
        Enter Your Last Name 
        <input type="text" name="last_name">
      </label>
      <br>
      <label for="email">
        Enter Your Email 
        <input type="text" name="email">
      </label>
      <br>
        <label for="password">
        Enter A Password 
        <input type="password" name="password">
      </label>
      <br>
      <input type="submit" value="Sign up">
    </form>

  </body>
</html>

Upvotes: 1

Views: 913

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

While flash is valid inside your controller, Capybara deals with what is on the page, which is what you should be checking, so flash has no meaning. In your erb you are putting the flash notice inside an h2 so the test for that would be

expect(page).to have_css('h2', text: "Please ensure you have filled all the fields")

Going forward you might want to add an id to the container (h2) of the flash messages so you can more excatly target it.

Upvotes: 2

Related Questions