Rspec issues with Devise - devise

In my tickets_controller.rb:
def create
#ticket = #project.tickets.build(ticket_params)
#ticket.author = current_user
if #ticket.save
flash[:notice] = "Ticket has been created."
redirect_to [#project, #ticket]
else
flash.now[:alert] = "Ticket has not been created."
render "new"
end
end
So I assumed, I should be OK to pass the test, but it's giving me the errors below.
I'm under impression it's not invoking the email address from current_user.email...
The repo here https://github.com/tenzan/ticketee.
Deployed version here https://github.com/tenzan/ticketee
$ rspec spec/features/creating_tickets_spec.rb
...F
Failures:
1) Users can create new tickets with valid attributes
Failure/Error: expect(page).to have_content "Author: #{user.email}"
expected to find text "Author: test4#example.com" in "Internet Explorer Non-standards compliance My pages are ugly!"
# ./spec/features/creating_tickets_spec.rb:36:in `block (3 levels) in <top (required)>'
# ./spec/features/creating_tickets_spec.rb:35:in `block (2 levels) in <top (required)>'
Finished in 0.64558 seconds (files took 1.3 seconds to load)
4 examples, 1 failure
Failed examples:
rspec ./spec/features/creating_tickets_spec.rb:30 # Users can create new tickets with valid attributes
UPDATE 1:
show.html.erb for ticket:
<header>
<h2><%= #ticket.name %></h2>
<ul class="actions">
<li><%= link_to "Edit Ticket", [:edit, #project, #ticket],
class: "edit" %></li>
<li><%= link_to "Delete Ticket", [#project, #ticket], method: :delete,
data: { confirm: "Are you sure you want to delete this ticket?"},
class: "delete" %></li>
</ul>
</header>
<table id="attributes">
<tr>
<th>Author: </th>
<td><%= #ticket.author.email %></td>
</tr>
<tr>
<th>Created: </th>
<td><%= time_ago_in_words(#ticket.created_at) %> ago</td>
</tr>
</table>
<div id="ticket">
<header>
<h1><%= #project.name %></h1>
</header>
<header>
<h2><%= #ticket.name %></h2>
</header>
<%= simple_format(#ticket.description) %>
</div>
creating_tickets_specs.rb:
require 'rails_helper'
RSpec.feature 'Users can create new tickets' do
let(:user) { FactoryGirl.create(:user) }
before do
login_as(user)
project = FactoryGirl.create(:project, name: "Internet Explorer")
visit project_path(project)
click_link "New Ticket"
end
scenario "with valid attributes" do
fill_in "Name", with: "Non-standards compliance"
fill_in "Description", with: "My pages are ugly!"
click_button "Create Ticket"
expect(page).to have_content "Ticket has been created."
within("#ticket") do
expect(page).to have_content "Author: #{user.email}"
end
end
scenario "when providing invalid attributes" do
click_button "Create Ticket"
expect(page).to have_content "Ticket has not been created."
expect(page).to have_content "Name can't be blank"
expect(page).to have_content "Description can't be blank"
end
scenario "with an invalid description" do
fill_in "Name", with: "Non-standards compliance"
fill_in "Description", with: "It sucks"
click_button "Create Ticket"
expect(page).to have_content "Ticket has not been created."
expect(page).to have_content "Description is too short"
end
end

A couple things
After you set author on ticket, are you calling save?
What is in the show template where it's blowing up?
Typically I would write your controller code as:
current_user.tickets.create ticket_params
Which, assuming the relationships are set up correctly, will automatically set up the relationships as you would expect.

Rspec was looking for "Author: test4#example.com" within ticket tag as per within("#ticket") as described in the creating_tickets_spec.rb, but in my case it was out of that scope.
So, putting it inside solved the issue.

Related

Hartl tutorial chap. 9.3.3. pagination test fail

I would appreciate any help why my test is failing. I am doing it based on Michael Hartl tutorial and I tried almost everything and read a lot of about it, but still clueless.
Tahnks
My test:
describe "pagination" do
before(:all) do
sign_in FactoryGirl.create(:user)
30.times { FactoryGirl.create(:user) }
visit users_path
end
after(:all) { User.delete_all }
it 'has the right div' do
page.should have_selector('div.pagination')
end
it "should list each user" do
User.paginate(page: 1).each do |user|
expect{page}.to have_selector('li', text: user.username)
end
end
My view:
<h1>Všichni uživatelé</h1>
<%= will_paginate %>
<ul class="users">
<% #users.each do |user| %>
<li>
<%= gravatar_for user%>
<%= link_to user.username, user %>
</li>
<% end %>
</ul>
<%= will_paginate(:renderer => BootstrapPagination::Rails)%>
My controller:
def index
#users = User.paginate(page: params[:page])
end
My error:
1) User pages index pagination should list each user
Failure/Error: expect{page}.to have_selector('li', text: user.username)
expected css "li" with text "Person 44" to return something
# ./spec/requests/user_pages_spec.rb:122:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:121:in `block (4 levels) in <top (required)>'
I think the line:
expect{page}.to have_selector('li', text: user.username)
Should be:
expect{page}.to have_selector('li', text: user.user.name)

why does this cucumber code fail

I have this code : https://github.com/roelof1967/tamara
But when I do cucumber it fails with this error message : https://gist.github.com/4008123
Can anyone explain to me what's wrong with my code?
Roelof
Edit 1:
Code that fails:
class Output
def messages
#messages ||= []
end
def puts(message)
messages << message
end
def output
#output ||= Output.new
end
end
When /^he logs in$/ do
visit("/users/sign_in")
fill_in('Email', :with => #user.email)
fill_in('Password', :with => #user.password)
click_button('Sign in')
end
Then /^he should see "(.*?)"$/ do |message|
#output.messages.should include (message)
end
And here is the error message :
undefined method `[]' for nil:NilClass NoMethodError)
./features/step_definitions/login_steps.rb:6:in `/^he logs in$/'
features/login.feature:5:in `When he logs in'
Do this for your solution:
Step 1 : create home controller
class HomeController < ApplicationController
def index
end
end
step 2 : create home folder in view and create index.html.erb file and your content look like as a example
<% if current_user.present? %>
<h1> <%= link_to "Sign Out", destroy_user_session_path, :method => :delete %> </h1>
<% else %>
<h1> <%= link_to "Sign Up", "/users/sign_up" %> |<%= link_to "Sign In", user_session_path %> </h1>
<% end %>
step 3 : now your login.feature file look like
Feature: login and authecation
Scenario: log in as existing user
Given a user "roelof" exists
When he logs in
Then he should see "Signed in successfully."
And he should see "Sign Out"
Scenario: log in with bad password
Given a user "Aslak" exists
When he logs in with a bad password
Then he should not see "Welcome, Aslak"
And he should see "Login failed"
step 4 now, run cucumber
your scenario "log in as existing user" will pass.

Hartl Rails Tutorial (3.2) 8.2.5 Rspec failure

UPDATE:
My test was not submitting the user info. The relavant code was in the previous chapter's exercises:
describe "after saving user" do
before { click_button submit }
let(:user) { User.find_by_email('user#example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
it { should have_link('Profile') }
end
/UPDATE
I've completed section 8.2.5 (Signin upon signup) and the app behaves exactly as described:
the user is signed-in upon signup
then redirected to their profile page
where the header has been changed to include a 'Sign out' link.
But my test for the 'Sign out' link fails. Here's my code, all copied from the tutorial:
relevant controller code (users_controller.rb):
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
relevant view code (_header.html.erb):
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
relevant test code (user_pages_spec.rb):
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
describe "after saving user" do
it { should have_link('Profile') }
end
end
end
The error is rspec ./spec/requests/user_pages_spec.rb:47 # User pages signup with valid information after saving user
Thanks!
I think the last 'describe' block should look like this:
describe "after saving user" do
before { click_button submit }
it { should have_content('Profile') }
end
The test missed clicking the "submit" button before examining if there is appropriate content on a page.

devise rails - error messages in different view?

I'm trying to show my errors (both flash and devise) in one of my layout files (_header.html.erb):
<div id="alerts">
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
<% end %>
<%= devise_error_messages! %>
</div>
but I'm getting an error: undefined local variable or method `resource', because the error message is no longer in the devise form. I tried the method suggested here: http://pupeno.com/2010/08/29/show-a-devise-log-in-form-in-another-page/ by pasting that code into the application_controller.rb file. No luck. Ideas?
oh. I forgot to mention... The pages work without error, but my tests are failing... Here's a failing test:
it 'succeeds with a valid email and password' do
user = Factory.create(:user)
visit sign_in_path
fill_in 'user_email', :with => user.email
fill_in 'user_password', :with => user.password
click_button 'Sign in'
page.should have_content("hi #{user.username}")
end
This is Rails3, fyi.
I was getting errors like this and solved using :partial and :locals when using the render method:
<%= render :partial => 'header', :locals => { :flash => flash } %>
See Rails 3: "undefined local variable or method" if I put content in a partial
turns out the code needed to be in the application_helper instead of the controller, but the code at pupeno.com works!

Undefined Method 'NameOfField' for #<Model:0x000...>

I'm totally stumped on this error. Would really appreciate some help :).
To reproduce the error, you can pull the program from https://github.com/WaleyChen/twitter_clone. Then run 'bundle exec rspec spec/'.
I have an rspec test for my controller defined as:
require 'spec_helper'
describe FrontpageController do
render_views # render the views inside the controller tests, so not just test the actions
describe "GET 'frontpage'" do
it "should be successful" do
get 'frontpage'
response.should be_success
end
it "should have the right title" do
get 'frontpage'
response.should have_selector("title", :content => "Twitter")
end
end
end
When I run my rspec tests, I get the following error:
Failures:
1) FrontpageController GET 'frontpage' should be successful
Failure/Error: get 'frontpage'
ActionView::Template::Error:
undefined method `full_name' for #<User:0x007fbfce43dce0>
# ./app/views/frontpage/frontpage.html.erb:22:in `block in _app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
# ./app/views/frontpage/frontpage.html.erb:21:in `_app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
# ./spec/controllers/frontpage_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
2) FrontpageController GET 'frontpage' should have the right title
Failure/Error: get 'frontpage'
ActionView::Template::Error:
undefined method `full_name' for #<User:0x007fbfcc99a410>
# ./app/views/frontpage/frontpage.html.erb:22:in `block in _app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
# ./app/views/frontpage/frontpage.html.erb:21:in `_app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
# ./spec/controllers/frontpage_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
Here's the controller:
class FrontpageController < ApplicationController
def frontpage
#user = User.new
#sign_up = User.new
end
def sign_up
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
end
end
end
Here's the view, that's causing the error:
<%= form_for #user, :url =>"sign_up" do |form| %>
<%= form.text_field :full_name, :placeholder => "Full name" %>
</br>
<%= form.text_field :email, :placeholder => "Email" %>
</br>
<%= form.text_field :pw, :placeholder => "Password" %>
</br>
<%= form.text_field :username, :placeholder => "Username" %>
</br>
<%= form.submit "Sign up" %>
<% end %>
Here's user.rb:
class User < ActiveRecord::Base
end
Here's schema.rb:
ActiveRecord::Schema.define(:version => 20111106084309) do
create_table "users", :force => true do |t|
t.string "full_name"
t.string "email"
t.string "username"
t.string "pw"
t.datetime "created_at"
t.datetime "updated_at"
end
end
You need to make sure your test database is up to date with all the migrations:
rake db:test:prepare
However, you may have a problem with that as your migrations are broken; you have two migrations named "CreateUsers" which Rails will complain about. It looks like you should delete the more recent one, and then uncomment the t.string :email line in the original one.
Also, if you use bundle exec rake rspec instead of bundle exec rspec spec it'll make sure your database migrations are up to date before running the tests. I just cloned your repo and did this and it passed the tests just fine.