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: 'chris@chris.com'
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>