Pre-processing within a Rails Controller create method does not work - ruby-on-rails

I am following Michael Hartl's tutorial, and trying to implement the reply twitter-like functionality, ie. "#122-john-smith: hello there" should be a reply to user 122.
I first tried filtering the "#XXX-AAA-AAA" part using a before_filter, but I decided to try it first in the very same Micropost#create action. So far I've got this MicropostController:
class MicropostsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: [:destroy]
#before_filter :reply_to_user, only: [:create]
def index
end
def create
#micropost=current_user.microposts.build(params[:micropost])
#Rails.logger.info "hoooola"
regex=/\A#(\d)+(\w|\-|\.)+/i
message=#micropost.content.dup
isResponse=message.match(regex)[0].match(/\d+/)[0]
#micropost.response=isResponse
if #micropost.save
flash[:success]="Micropost created!"
redirect_to root_path
else
#feed_items=[]
render 'static_pages/home'
end
end
def destroy
#micropost.destroy
redirect_to root_path
end
private
def correct_user
#micropost = current_user.microposts.find_by_id(params[:id])
redirect_to root_path if #micropost.nil?
end
def reply_to_user
regex=/\A#(\d)+(\w|\-|\.)+/i
#I use [0] cause the output of match is a MatchData class with lots of bs
mtch=params[:micropost][:content].match(regex)[0]
#puts mtch
##micropost=current_user.microposts.build(params[:micropost])
if mtch != nil
user_id=mtch.match(/\d+/)[0]
#replied_user=User.find(user_id)
#micropost.response=user_id unless #replied_user.nil?
end
end
end
And this is the snippet test I'm trying to pass:
require 'spec_helper'
describe "MicropostPages" do
subject { page }
let(:user) { FactoryGirl.create(:user) }
before { valid_signin user }
describe "micropost creation" do
before { visit root_path }
describe "with invalid information" do
it "should not create a micropost" do
expect { click_button "Post" }.should_not change(Micropost,
:count)
end
describe "error messages" do
before { click_button "Post" }
it { should have_content('error') }
end
end
describe "with valid information" do
before { fill_in 'micropost_content', with: "Lorem ipsum" }
it "should create a micropost" do
expect { click_button "Post" }.should change(Micropost,
:count).by(1)
end
end
end
...
end
If I run these tests I get the follwing error:
Failures:
1) MicropostPages micropost creation with invalid information should not create a micropost
Failure/Error: expect { click_button "Post" }.should_not change(Micropost, :count)
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/microposts_controller.rb:14:in `create'
# (eval):2:in `click_button'
# ./spec/requests/micropost_pages_spec.rb:11:in `block (5 levels) in <top (required)>'
# ./spec/requests/micropost_pages_spec.rb:11:in `block (4 levels) in <top (required)>'
2) MicropostPages micropost creation with invalid information error messages
Failure/Error: before { click_button "Post" }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/microposts_controller.rb:14:in `create'
# (eval):2:in `click_button'
# ./spec/requests/micropost_pages_spec.rb:14:in `block (5 levels) in <top (required)>'
However if I modify the tests and comment out all the #XXX filtering in the Micropost#create action:
def create
#micropost=current_user.microposts.build(params[:micropost])
#Rails.logger.info "hoooola"
#regex=/\A#(\d)+(\w|\-|\.)+/i
#message=#micropost.content.dup
#isResponse=message.match(regex)[0].match(/\d+/)[0]
##micropost.response=isResponse
if #micropost.save
flash[:success]="Micropost created!"
redirect_to root_path
else
#feed_items=[]
render 'static_pages/home'
end
end
The tests pass just fine and the new Micropost is not a Nil object.
It can't seem to find an explanation here.

The error comes from this line:
isResponse=message.match(regex)[0].match(/\d+/)[0]
Check if your two match calls actually match correctly. If the pattern is not found in your string, nil is returned and the [0] call is made on nil. There's two instances in this line alone where this could happen.
Try to spread it out over several lines and check the return values of your matches or extend your Regex to properly check the pattern in one go.

Related

RSpec controller test errors within embedded resource

I was writing tests for my app using responders gem.
Here are my routes:
resources :sites do
resources :pages, shallow: true
end
My PagesController chunk of code:
def create
respond_with(#page = #site.pages.create(page_params))
end
def find_site
#site = current_user.sites.find(params[:site_id])
end
And tests that are failing:
sign_in_user
let(:user_2) { create(:user) }
let(:site) { create(:site, user: #user) }
let(:page) { create(:page, site: site, user: #user) }
describe 'POST #create' do
context 'with valid attributes' do
it 'associates new page with the site' do
expect { post :create, params: { page: attributes_for(:page), site_id: site } }.to change(site.pages, :count).by(1)
end
it 'redirects to show view' do
post :create, params: { page: attributes_for(:page), site_id: site }
expect(response).to redirect_to page_path(assigns(:page))
end
end
Errors are following:
1) PagesController POST #create with valid attributes associates new page with the site
Failure/Error: expect { post :create, params: { page: attributes_for(:page), site_id: site } }.to change(site.pages, :count).by(1)
expected #count to have changed by 1, but was changed by 0
# ./spec/controllers/pages_controller_spec.rb:37:in `block (4 levels) in <top (required)>'
2) PagesController POST #create with valid attributes redirects to show view
Failure/Error: expect(response).to redirect_to page_path(assigns(:page))
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"pages", :id=>nil}, missing required keys: [:id]
# ./spec/controllers/pages_controller_spec.rb:42:in `block (4 levels) in <top (required)>'
If I change site.pages in first test to Page - it's actually working.
So I am really confused how to fix this tests and where is the mistake.
Solved
Problem was with my PagesController, method create should look like this
def create
#page = #site.pages.build(page_params)
#page.user = current_user
#page.save
respond_with(#page)
end
Problem was with my PagesController, method create should look like this
def create
#page = #site.pages.build(page_params)
#page.user = current_user
#page.save
respond_with(#page)
end

Test logic with rspec

I have a controller in application:
class CartsController < ApplicationController
def show
#cart = Cart.find(session[:cart_id])
#products = #cart.products
end
end
and i wrote some initial spec to test response using rspec
RSpec.describe CartsController, type: :controller do
describe 'GET #show' do
before do
get :show
end
it { expect(response.status).to eq(200) }
it { expect(response.headers["Content-Type"]).to eql("text/html; charset=utf-8")}
it { is_expected.to render_template :show }
end
end
Now I am going to test show method logic and should write some expectation like:
it 'should be products in current cart' do
end
but I have no idea how to pass cart.id to the session hash
Update! I am trying to write product instances what will be associated with current cart:
let(:products_list){FactoryGirl.build_list(:product, cart_id: session[:cart_id])}
let(:cart){FactoryGirl.build(:cart)}
...
it 'should be products in current cart' do
session[:cart_id] = cart.id
expect(assigns(:products)).to eq([products_list])
end
but got an error:
CartsController GET #show should be products in current cart
Failure/Error: let(:cart){FactoryGirl.build(:cart)}
ArgumentError:
Trait not registered: products
# ./spec/controllers/carts_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
# ./spec/controllers/carts_controller_spec.rb:15:in `block (3 levels) in <top (required)>'
Something still going wrong
you can set the session in your controller test
it 'should be products in current cart' do
session[:cart_id] = 10
get :show
end

Hartl Chapter 11 Ajax undefined local variable or method `cookies'

I am doing the Hartl Tutorial, and am currently in Chapter 11.2.5 A working follow button with AJAX.
My tests are failing for the Relationship Controller and I can't figure out why. The expected behavior happens in the browser. If I remove the cookies method it causes many other tests to fail. What am I missing that is causing this issue?
Here are my tests for the RelationshipController
require 'spec_helper'
describe RelationshipsController do
let(:user) { FactoryGirl.create(:user) }
let(:other_user) { FactoryGirl.create(:user) }
before { sign_in user, no_capybara: true }
describe "creating a relationship with Ajax" do
it "should increment the Relationship count" do
expect do
xhr :post, :create, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)
end
it "should respond with success" do
xhr :post, :create, relationship: { followed_id: other_user.id }
expect(response).to be_success
end
end
describe "destroying a relationship with Ajax" do
before { user.follow!(other_user) }
let(:relationship) { user.relationships.find_by(followed_id: other_user) }
it "should decrement the Relationship count" do
expect do
xhr :delete, :destroy, id: relationship.id
end.to change(Relationship, :count).by(-1)
end
it "should respond with success" do
xhr :delete, :destroy, id: relationship.id
expect(response).to be_success
end
end
end
Here is my Utilities.rb
include ApplicationHelper
RSpec::Matchers.define :have_error_message do |message|
match do |page|
expect(page).to have_selector('div.alert.alert-error', text: message)
end
end
def sign_in(user, options={})
if options[:no_capybara]
# Sign in when not using Capybara.
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
Here is my relationships_controller.rb
class RelationshipsController < ApplicationController
before_action :signed_in_user
def create
#user = User.find(params[:relationship][:followed_id])
current_user.follow!(#user)
respond_to do |format|
format.html { redirect_to #user }
format.js
end
end
def destroy
#user = Relationship.find(params[:id]).followed
current_user.unfollow!(#user)
respond_to do |format|
format.html { redirect_to #user }
format.js
end
end
end
Here is my tests output:
Failures:
1) RelationshipsController creating a relationship with Ajax should increment the Relationship count
Failure/Error: before { sign_in user, no_capybara: true }
NameError:
undefined local variable or method `cookies' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fc7f827f930>
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/models/relationship_spec.rb:8:in `block (2 levels) in <top (required)>'
2) RelationshipsController creating a relationship with Ajax should respond with success
Failure/Error: before { sign_in user, no_capybara: true }
NameError:
undefined local variable or method `cookies' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fc7f8516a90>
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/models/relationship_spec.rb:8:in `block (2 levels) in <top (required)>'
3) RelationshipsController destroying a relationship with Ajax should decrement the Relationship count
Failure/Error: before { sign_in user, no_capybara: true }
NameError:
undefined local variable or method `cookies' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fc7f85367f0>
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/models/relationship_spec.rb:8:in `block (2 levels) in <top (required)>'
4) RelationshipsController destroying a relationship with Ajax should respond with success
Failure/Error: before { sign_in user, no_capybara: true }
NameError:
undefined local variable or method `cookies' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fc7f8556938>
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/models/relationship_spec.rb:8:in `block (2 levels) in <top (required)>'
Finished in 0.06635 seconds
4 examples, 4 failures
Failed examples:
rspec ./spec/models/relationship_spec.rb:12 # RelationshipsController creating a relationship with Ajax should increment the Relationship count
rspec ./spec/models/relationship_spec.rb:18 # RelationshipsController creating a relationship with Ajax should respond with success
rspec ./spec/models/relationship_spec.rb:29 # RelationshipsController destroying a relationship with Ajax should decrement the Relationship count
rspec ./spec/models/relationship_spec.rb:35 # RelationshipsController destroying a relationship with Ajax should respond with success
I noticed you put your test of RelationshipsController in spec/models. Is that really what the tutorial says to do? Try putting it under spec/controllers so it is treated as a controller spec.
I have just been struggling with this issue, as well as a lot of other people by the looks of things.
My solution (which I suspect is not the right one) was to change the line :
before { sign_in user, no_capybara: false }
to
before { sign_in user }
Not sure why, but it looks like my rspec test can not see the code in utilities.rb

Rails testing: "No route matches" for custom route

I have following route:
GET /confirm/:token(.:format) Confirmations#confirm
Controller:
class ConfirmationsController < ApplicationController
# GET /confirm/<token>
def confirm
#user = User.find_by_email_token(params[:token])
if #user
#user.confirmed = true
#user.email_token = nil
#user.save!
sign_in #user
redirect_to root_url, flash: { success: "Welcome <#{#user.email}>, your address has been verified." }
elsif
redirect_to root_url, flash: { error: "Error: could not find matching user record." }
end
end
end
And this simple confirmations_controller_spec.rb:
require 'spec_helper'
describe ConfirmationsController do
let(:user) { FactoryGirl.create(:user, email_token: "some_token") }
describe "Get confirm" do
it "confirms user with valid email_token" do
get :confirm, token: "some_token"
assigns(:user).should eq(user)
user.reload.email_token.should be_nil
end
it "does not confirm user with invalid email_token"
end
end
but it fails:
1) ConfirmationsController Get confirm confirms user with valid email_token
Failure/Error: get :confirm, token: "some_token"
ActionController::RoutingError:
No route matches {:token=>"some_token", :controller=>"confirmations", :action=>"confirm"}
# ./spec/controllers/confirmations_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
Anyone see what (could be multiple things) I screwed up?
BTW- I'm using a get request here (as opposed to put) because it's being initiated from a text based email so we can't, to my understanding, use a put request...
In your rake routes, Confirmations should not have a capital letter.
Can you define the route like so in config/routes.rb:
match '/confirm/:token' => 'confirmations#confirm'

Rspec tests unable to show successful situations

I've got the following Rspec tests that test whether a user can successfully update a scavenger hunt. I know the site will let the user do this, but my problem is that my tests aren't capturing that. Each time I run rspec, all the tests below fail. I'm hoping there's just one simple thing that I'm forgetting to put in there, but my eyes are bleary all the code's running together. When you look at these tests, does anything strike you as obviously wrong?
describe "PUT 'update'"
before(:each) do
#hunt = FactoryGirl.create(:hunt)
end
....
describe "as an admin user" do
before(:each) do
admin = FactoryGirl.create(:user, :email => "admin#example.com", :admin => true)
test_sign_in(admin)
end
....
describe "success" do
before(:each) do
#attr = { :name => "New Hunt" }
end
it "returns http success" do
get 'edit', :id => #hunt
response.should be_success
end
it "should change the hunt's name" do
put :update, :id => #hunt, :name => #attr
#hunt.reload
#hunt.name.should == #attr[:name]
end
it "should redirect to the hunt show page" do
put :update, :id => #hunt
response.should redirect_to(#hunt)
end
it "should have a flash message" do
put :update, :id => #hunt, :user => #attr
flash[:success].should =~ /updated/
end
end
...
end
Here's code from my controller.
def edit
#hunt = Hunt.find(params[:id])
#title = "Edit hunt"
end
def update
#hunt = Hunt.find(params[:id])
if #hunt.update_attributes(params[:hunt])
flash[:success] = "Hunt updated."
redirect_to hunts_path
else
#title = "Edit Hunt"
render 'edit'
end
end
And here's the feedback I'm getting form Rspec. It's all across the boards, but I'm hoping it's caused by a single problem and not four individual ones.
1) HuntsController PUT 'update' as an admin user failure should render the 'edit' page
Failure/Error: response.should render_template('edit')
expecting <"edit"> but rendering with <"">
# ./spec/controllers/hunts_controller_spec.rb:220:in `block (5 levels) in <top (required)>'
2) HuntsController PUT 'update' as an admin user failure should have the right title
Failure/Error: response.should have_selector("title", :content => "Edit hunt")
expected following output to contain a <title>Edit hunt</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>You are being redirected.</body></html>
# ./spec/controllers/hunts_controller_spec.rb:226:in `block (5 levels) in <top (required)>'
3) HuntsController PUT 'update' as an admin user success should change the hunt's name
Failure/Error: #hunt.name.should == #attr[:name]
expected: "New Hunt"
got: "Hunt 9" (using ==)
# ./spec/controllers/hunts_controller_spec.rb:244:in `block (5 levels) in <top (required)>'
4) HuntsController PUT 'update' as an admin user success should redirect to the hunt show page
Failure/Error: response.should redirect_to(#hunt)
Expected response to be a redirect to <http://test.host/hunts/649> but was a redirect to <http://test.host/hunts>
# ./spec/controllers/hunts_controller_spec.rb:249:in `block (5 levels) in <top (required)>'

Resources