I have a Rspec Test im trying to run. It did the test manually and once you click Create Size it definitely goes to sizes_path. Why is it going to root url?
Error is
Failure/Error: expect(current_path).to eql(sizes_path)
expected: "/sizes"
got: "/"
(compared using eql?)
Test is
require "rails_helper"
require "when_authenticated.rb"
RSpec.feature "adding size" do
let(:size01) { FactoryGirl.build :size01 }
let(:user) { FactoryGirl.build :user }
let(:admin) { FactoryGirl.create(:user, admin: true, password: "Password18") }
scenario "allow a admin user to add a size" do
admin_logged_in
visit new_size_path
fill_in 'Title', with: 'example'
click_button 'Create Size'
expect(current_path).to eql(sizes_path)
expect(page).to have_content("example")
expect(page).to have_content("You have created a new size")
end
scenario "user can't add size" do
user_logged_in
visit sizes_path
expect(current_path).to eql(root_path)
end
scenario "vistor can't add size" do
visit sizes_path
expect(current_path).to eql(root_path)
end
end
Here is my Create Method in Sizes controller
def create
#size = Size.new(size_params)
if #size.save
redirect_to sizes_path
flash[:success] = "You have created a new size"
else
render 'new'
end
end
New View
<center><h1>Create A New Size</h1></center>
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_form_for #size do |f| %>
<%= f.input :title %>
<%= f.button :submit, "Create Size", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
The problem was with the way my admin
Here is the fix in total
require "rails_helper"
# require "when_authenticated.rb"
RSpec.feature "adding size" do
let(:size01) { FactoryGirl.build :size01 }
let(:user) { FactoryGirl.build :user }
let(:admin) { FactoryGirl.create(:user, admin: true, password: "Password18" ) }
def admin_logged_in
visit login_path
fill_in 'Email', with: admin.email
fill_in 'Password', with: admin.password
click_button 'Log In'
end
def user_logged_in
visit login_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Log In'
end
scenario "allow a admin user to add a size" do
admin_logged_in
visit new_size_path
fill_in 'Title', with: 'example'
click_button('Create Size')
expect(current_path).to eql(sizes_path)
expect(page).to have_content("List Of Sizes")
expect(page).to have_content("You have created a new size")
end
scenario "user can't add size" do
user_logged_in
visit sizes_path
expect(current_path).to eql(root_path)
end
scenario "vistor can't add size" do
visit sizes_path
expect(current_path).to eql(root_path)
end
end
Related
I am facing this error when running an rspec test. I have tried click_link "Title 1" and assigning Article.create to a variable but the same error appears.
Failure/Error: click_link article.title
Capybara::ElementNotFound:
Unable to find link "Title 1"
comment_spec.rb
require "rails_helper"
describe 'navigate' do
let(:user) { FactoryGirl.create(:user) }
let(:article) do
Article.create(title: "Title 1", description: "Some description", user_id: user.id)
end
before do
login_as(user, :scope => :user)
end
describe 'create' do
before do
visit articles_path
end
it"permits a signed in user to write a review" do
click_link article.title
fill_in "Content", with: "An awesome article"
click_button "Post"
expect(page).to have_content("An awesome article")
expect(current_path).to eq(article_path(article.id))
end
end
end
spec/factories/user.rb
FactoryGirl.define do
sequence :email do |n|
"test#{n}#example.com"
end
factory :user do
name 'Tester'
email { generate :email }
password "asdfasdf"
password_confirmation "asdfasdf"
end
EDIT:
article/index.html.erb
<div class="tab-pane active" id="tab1">
<% #articles.each do |article| %>
<%= render 'article', article: article %>
<% end %>
</div>
_article.html.erb
<%= link_to article.title, article_path(article) %>
let is lazy loading which means that the block is not evaluated until you call it.
before do
article # calls the block
visit articles_path
end
An alternative is to use let! which is not lazy loading:
require "rails_helper"
describe 'navigate' do
let(:user) { FactoryGirl.create(:user) }
let!(:article) do
Article.create(title: "Title 1", description: "Some description", user_id: user.id)
end
before do
login_as(user, :scope => :user)
end
describe 'create' do
before do
visit articles_path
end
it"permits a signed in user to write a review" do
click_link article.title
fill_in "Content", with: "An awesome article"
click_button "Post"
expect(page).to have_content("An awesome article")
expect(current_path).to eq(article_path(article.id))
end
end
end
I have a Rspec Test im trying to run. It did the test manually and once you click Create Size it definitely goes to sizes_path. Why is it going to root url?
Error is
Failure/Error: expect(current_path).to eql(sizes_path)
expected: "/sizes"
got: "/"
(compared using eql?)
Test is
require "rails_helper"
require "when_authenticated.rb"
RSpec.feature "adding size" do
let(:size01) { FactoryGirl.build :size01 }
let(:user) { FactoryGirl.build :user }
let(:admin) { FactoryGirl.create(:user, admin: true, password: "Password18") }
scenario "allow a admin user to add a size" do
admin_logged_in
visit new_size_path
fill_in 'Title', with: 'example'
click_button 'Create Size'
expect(current_path).to eql(sizes_path)
expect(page).to have_content("example")
expect(page).to have_content("You have created a new size")
end
scenario "user can't add size" do
user_logged_in
visit sizes_path
expect(current_path).to eql(root_path)
end
scenario "vistor can't add size" do
visit sizes_path
expect(current_path).to eql(root_path)
end
end
Here is my Create Method in Sizes controller
def create
#size = Size.new(size_params)
if #size.save
redirect_to sizes_path
flash[:success] = "You have created a new size"
else
render 'new'
end
end
New View
<center><h1>Create A New Size</h1></center>
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_form_for #size do |f| %>
<%= f.input :title %>
<%= f.button :submit, "Create Size", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
For some reason admin wasn't being created properly. Here is the fix.
require "rails_helper"
RSpec.feature "adding size" do
let(:size01) { FactoryGirl.build :size01 }
let(:user) { FactoryGirl.build :user }
let(:admin) { FactoryGirl.create(:user, admin: true, password: "Password18" ) }
def admin_logged_in
visit login_path
fill_in 'Email', with: admin.email
fill_in 'Password', with: admin.password
click_button 'Log In'
end
def user_logged_in
visit login_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Log In'
end
scenario "allow a admin user to add a size" do
admin_logged_in
visit new_size_path
fill_in 'Title', with: 'example'
click_button('Create Size')
expect(current_path).to eql(sizes_path)
expect(page).to have_content("List Of Sizes")
expect(page).to have_content("You have created a new size")
end
scenario "user can't add size" do
user_logged_in
visit sizes_path
expect(current_path).to eql(root_path)
end
scenario "vistor can't add size" do
visit sizes_path
expect(current_path).to eql(root_path)
end
end
I'm stuck at Chapter 7 in the Michael Hartl's Ruby on Rails tutorial.
My routes.rb
SampleApp::Application.routes.draw do
get "users/new"
resources :users
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
My spec/requests/user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
let(:submit) { "Create my account" }
let(:submit) { "Sign up" }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
My app/views/users/new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
And when I try to run test I get this:
gvyntyk#gvyntyk-r60:~/rails_projects/sample_app$ bundle exec rspec spec/
...............FF......................
Failures:
1) User pages with invalid information should not create a user
Failure/Error: expect { click_button submit }.not_to change(User, :count)
NameError:
undefined local variable or method `submit' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_3:0xad40120>
# ./spec/requests/user_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:27:in `block (3 levels) in <top (required)>'
2) User pages with valid information should create a user
Failure/Error: fill_in "Name", with: "Example User"
Capybara::ElementNotFound:
Unable to find field "Name"
# ./spec/requests/user_pages_spec.rb:33:in `block (3 levels) in <top (required)>'
Finished in 1.54 seconds
39 examples, 2 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:26 # User pages with invalid information should not create a user
rspec ./spec/requests/user_pages_spec.rb:39 # User pages with valid information should create a user
Can someone please explain why this test is failing?
Please make sure that you are at a point on the chapter where the tests should pass. There are some tests that are not supposed to pass after you change the code.
Also, your second error seems to be an authentication issue. Unable to find field "Name" could mean that at the time that this test is running the user should have have sign_up. I am not quite sure on the fragment you posted, but make sure that all of your indentation is correct and your 'ends' are closing in the right place. The way you have the code right now it does not look like you are signing up before executing the "with valid information". Try placing the "end" of the describe "signup page" do, at the very bottom before the "end" of the "describe "User pages" do" block.
From click_button docs:
Finds a button by id, text, value or title and clicks it.
So you can supply button value to click_button method. Instead of:
expect { click_button submit }.not_to change(User, :count)
it should be:
expect { click_button 'Create my account' }.not_to change(User, :count)
And you receive this error:
Failure/Error: fill_in "Name", with: "Example User"
Capybara::ElementNotFound:
Unable to find field "Name"
Because you forgot to visit signup_path in both:
describe "with invalid information" do
and
describe "with valid information" do
blocks.
I ran rspec on some tests I created and here is the output I received.
Failures:
1) User pages signup with valid information should create a user
Failure/Error: fill_in "Name", with: "Example User"
Capybara::ElementNotFound:
Unable to find field "Name"
# ./spec/requests/user_pages_spec.rb:18:in `block (4 levels) in <top (required)>'
2) User pages signup with invalid information should not create a user
Failure/Error: expect { click_button submit }.not_to change(User, :count)
Capybara::ElementNotFound:
Unable to find button "Create my account"
# ./spec/requests/user_pages_spec.rb:12:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:12:in `block (4 levels) in <top (required)>'
Here is my rspec code for user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup" do
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
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
end
Now, to show you my html page
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.text_field :password %>
<%= f.label :password_confirmation %>
<%= f.text_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
In this example, clearly you can find a text-field named "Name" and a button "Create my account". I'm baffled at how rspec saw errors.
Can anyone help me?
Edit: Maybe it didn't work because I haven't defined a create method in my UsersController
class UsersController < ApplicationController
def new
#user = User.new
end
def show
#user = User.find(params[:id])
end
end
You are not actually going to any page before you start trying to fill in the form.
At the beginning of the test you will need a visit statement. Something like:
describe "User pages" do
subject { page }
describe "signup" do
before { visit new_user_path }
...
I'm getting the following error when testing the sign up page for the ruby-on-rails tutorial:
Failures:
1) User pages signup with valid information should create a user
Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
ActionView::MissingTemplate:
Missing template users/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "/Users/Fif/rails_projects/sample_app/app/views"
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:43:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:43:in `block (4 levels) in <top (required)>'
Finished in 0.76918 seconds
35 examples, 1 failure
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:42 # User pages signup with valid information should create a user
I'm not sure what the problem is, and I've gone over the code several times to make sure that it matches the examples in the book. I'm pretty sure that it has to do with the new.html.erb file
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
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
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end
new.html.erb
<%= provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
# Handle a successful save.
else
render 'new'
end
end
end
Short Answer:
It is not supposed to work (yet).
Continue the tutorial, the answer will come in section 7.4;
Slightly longer Answer:
When you fill in the form and push the submit-button, the create-event in the users_controller gets triggered. Since you don't have any code there (yet), Rails assumes that you just want to render create.html.erb, which doesn't exist (and never will).
In order to get this to work immediately, you have to add redirect_to #user right underneath if #user.save. Check Listing 7.25;
Jump down to the bottom of the tutorial and the question is answered. Include redirect_to #user right under your #Handle a successful save.