Rspec testing a controller using from to? - ruby-on-rails

Tested in browser and works fine. Test error says "expected result to have changed from 0 to 1, but did not change". Is this a factory issue or rspec issue? Why is it not changing?
Error:
Failures:
1) ShortLinksController Short links controller Clicking a short link increments the click counter by 1
Failure/Error: expect{ get :url_dispatch, { id: short_link.short_link } }.to change{short_link.click_counter}.from(0).to(1)
expected result to have changed from 0 to 1, but did not change
# ./spec/controllers/short_links_controller_spec.rb:34:in `block (4 levels) in <top (required)>'
Rspec:
it "increments the click counter by 1" do
short_link = create(:short_link)
expect{ get :url_dispatch, { id: short_link.short_link } }.to change{short_link.click_counter}.from(0).to(1)
end
Controller:
def url_dispatch
id = params[:id]
record = ShortLink.where(["short_link = ?", id]).first
if record.update(click_counter: record.click_counter + 1)
redirect_to record.redirect_to
else
render '/not_found'
end
end
Factory:
FactoryGirl.define do
factory :short_link do
redirect_to "http://google.com"
title "This is the google page"
short_link "xGh7u"
click_counter 0
owner Owner.create!(first_name: "Bob", last_name: "Diller", email: "bdiller#example.com")
end
end

per Fab's request, here is how I'm currently working around the issue.
context 'save invocations' do
before(:each) do
#org = create(:organization)
user = create(:user, organization: #org, is_admin: true)
sign_in user
end
it 'valid scenario' do
user2 = create(:user, organization: #org, is_admin: false)
put :update, id: user2, user: { is_admin: true }
user2.reload
expect(response).to have_http_status(204)
expect(user2.is_admin).to eq true
end
end
Here I'm calling user2.reload in order to get the updated attributes from the user2 factory.
I don't know why the expect{} syntax doesn't work for factories but you could refactor your code like this:
it "increments the click counter by 1" do
short_link = create(:short_link)
count = short_link.click_counter
get :url_dispatch, { id: short_link.short_link }
short_link.reload
expect(short_link.click_counter).to eq count + 1
end
Again I'm not saying this is best practice, I just couldn't find anything in the FactoryGirl documentation regarding RSpec 3 expect syntax in controllers that update attributes.

Related

rspec telling me that ".update' method is undefined

This is the topic resolver. I'm trying to update a field in learning_path table each time this query is executed for a student.
class Resolvers::Topic < GraphQL::Schema::Resolver
type Types::TopicType, null: true
description 'Returns the topic information'
argument :title, String, required: true
def resolve(title:)
user = context[:current_user]
ability = Ability.for(user)
topic = Topic.find_by(title: title)
if %w[student teacher admin].include?(user&.role) && ability.can?(:read, Topic)
if user&.role == 'student'
user.userable.learning_paths.find_by(subject_id: topic.subject_id).update(visited_at: DateTime.now)
end
if %w[student teacher].include?(user&.role) && !user&.userable&.subjects&.include?(topic.subject)
raise GraphQL::ExecutionError, 'This topic is not avaiable for you.'
end
topic
else
raise GraphQL::ExecutionError, 'You can't access this information.'
end
end
end
It works but the rspec is failling with this error:
1) LmsApiSchema topic when there's a current user returns an error if the student is not subscribe
Failure/Error: user.userable.learning_paths.find_by(subject_id: topic.subject_id).update(visited_at: DateTime.now)
NoMethodError:
undefined method `update' for nil:NilClass
topic rspec test that fails in testing:
context 'when there\'s a current user' do
let!(:student) { create(:student) }
let!(:user) { create(:user, userable: student) }
let!(:subject_a) { create(:subject) }
let!(:topic) { create(:topic, subject_id: subject_a.id) }
let!(:question) { create(:question, topic_id: topic.id) }
before do
create(:option, question_id: question.id)
prepare_context({ current_user: user })
end
it 'returns an error if the student is not subscribe' do
expect(graphql!['errors'][0]['message']).to eq('This topic is not avaiable for you.')
end
Just worked out. Added the & before setting the method for avoiding the nil error.
Figured it out thanks to this
user.userable.learning_paths.find_by(subject_id: topic.subject_id)&.update(visited_at: DateTime.now)

RSpec controller test can't get id params. I don't know how to fix this

Hi I was testing by rspec for rails app now. I have no idea to fix this problem.
here down below. anyone can fix this?
1) Items::AddToBasketsController Post #create succuss
Failure/Error: #item = Item.find(params[:item_id])
ActiveRecord::RecordNotFound:
Couldn't find Item with 'id'=category=nigiri&discription=test+test&img=%23%3CFile%3A0x00007fc54e9bfc38%3E&name=test1&price=301
# ./app/controllers/items/add_to_baskets_controller.rb:4:in `create'
# ./spec/controller/add_to_baskets_controller_spec.rb:43:in `block (4 levels) in <top (required)>'
# ./spec/controller/add_to_baskets_controller_spec.rb:42:in `block (3 levels) in <top (required)>'
add_to_baskets_controller_spec.rb
require 'rails_helper'
RSpec.describe Items::AddToBasketsController, type: :controller do
let(:user) { create(:user) }
let(:admin) { create(:admin) }
let(:first_item) { create(:first_item, admin_id: admin.id) }
let(:item_params) { attributes_for(:first_item) }
before do
login_user
end
describe "Post #create" do
it "succuss" do
basket = user.prepare_basket
expect do
post :create, params: { item_id: FactoryBot.attributes_for(:first_item) }
end.to change(basket.basket_items, :count).by(1)
end
end
end
add_to_baskets_controller.rb
class Items::AddToBasketsController < Items::ApplicationController
def create
basket = current_user.prepare_basket
#item = Item.find(params[:item_id])
basket.basket_items.create!(item_id: #item.id)
flash[:success] = "your item in basket"
redirect_to baskets_path
end
end
On the line:
post :create, params: { item_id: FactoryBot.attributes_for(:first_item) }
Your saying that the parameter item_id should be FactoryBot.attributes_for(:first_item), which is a hash with all the attributes of first_item.
Moreover, to get the ID, you'll need to first create the object. In other word:
item_id: FactoryBot.create(:first_item).id
Or simply, using your let:
item_id: first_item.id

rails rspec access other tables when testing

I'm pretty new to rails and find myself having some problems with understanding the relation between my database tables (i guess?).
My problem is the following:
I've got a Users table, containing information about users including their email addresses and another table containing "games" i'd like to manage those players played.
When players want to submit their games, they have to specify the users participating in the game by their email addresses. I'd like to validate whether or not those players really exist.
My Game model:
class Game < ActiveRecord::Base
attr_accessible :caster1, :caster2, :faction1, :faction2, :player1, :player2, :points, :won
before_save { |game| player1 = player1.downcase }
before_save { |game| player2 = player2.downcase }
validate :existence_of_both_players
...
(some more validations)
...
private
def existence_of_both_players
User.exists?(:email => :player1.downcase) && User.exists?(:email => :player2.downcase)
end
end
My test case is the following:
require 'spec_helper'
describe Game do
before do
#game = Game.new( player1: "foobar#example.com",
faction1: "Some faction",
caster1: "Some caster",
player2: "bazbaz#example.com",
faction2: "Some other faction",
caster2: "Some other caster",
points: 35,
won: true)
end
...
(some other specs)
...
describe "saving a game" do
let(:player1) { User.create(name: "Example1",
email: "example1#foo.bar",
password: "foobar",
password_confirmation: "foobar") }
let(:player2) { User.create(name: "Example2",
email: "example2#foo.bar",
password: "foobar",
password_confirmation: "foobar") }
describe "with invalid players" do
describe "when both players do not exist" do
before { #game.player1 = #game.player2 = "some_wrong_user#example.com" }
it { should_not be_valid }
end
describe "when player1 does not exist" do
before { #game.player2 = player2 }
it { should_not be_valid }
end
describe "when player2 does not exist" do
before { #game.player1 = player1 }
it { should_not be_valid }
end
end
describe "with valid players" do
before do
#game.player1 = player1
#game.player2 = player2
end
it { should be_valid }
end
end
end
(Sorry for the mass of code, I just thought it would help).
My test are failing, and I'm pretty sure it's obvious why, sadly not for me.
Failures:
1) Game saving a game with invalid players when both players do not exist
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/models/game_spec.rb:108:in `block (5 levels) in <top (required)>'
...
I really can not figure out, why this is not working. I read the rails book and watched several screencasts, but none of them explains my problem properly.
Since this is my first post on stackoverflow, please let me know, if this post is too verbose. Thanks in advance.
A custom validator method should call errors.add to signal an error. So in your case something like:
def existence_of_both_players
player_one_exists = User.find_by_email :player1.downcase
player_two_exists = User.find_by_email :player2.downcase
unless player_one_exists and player_two_exists
errors.add :game, "both players need to exist"
end
end
Read more in the guide for ActiveRecord Validations and Callbacks.

Why is my rspec test failing with ActionController::RoutingError:?

Basically I'm stumped as to why this particular test is failing. The page works fine when I go in and test it out by hand but the test keeps failing. First off, here's the error.
2) Setlist Pages Edit page adding a song
Failure/Error: click_button submit
ActionController::RoutingError:
No route matches {:action=>"edit", :controller=>"setlists", :id=>nil}
# ./app/controllers/allocations_controller.rb:15:in `create'
# (eval):2:in `click_button'
# ./spec/requests/setlist_pages_spec.rb:79:in `block (4 levels) in <top (required)>'
I realise that the id is set to nil but I know that when the test initially visits the page it renders because tests for content pass. I'm not sure why it points to the create controller when I'm testing the edit action as well? The test is shown below:
before do
#setlist = Setlist.create(date: Date.today, morning: true)
end
...
describe "Edit page" do
let(:admin) { FactoryGirl.create(:admin) }
let(:submit){ "Add Song" }
before do
#secondSong = FactoryGirl.create(:song)
sign_in admin
visit edit_setlist_path(#setlist)
end
it{ should have_content("Edit a Setlist")}
describe "adding a song" do
before do
select("#{#secondSong.title} by #{#secondSong.artist}", from: 'Songs')
click_button submit
end
it{ should have_selector('div.alert.alert-success')}
it "should create a new allocation" do
expect{click_button submit}.to change(Allocation, :count).by(1)
end
end # end adding a song
end # end edit test
Controller code as requested:
def create
#setlist = Setlist.new(params[:setlist])
if #setlist.save
#success!
flash[:success] = "Setlist saved"
##setlist.allocations.build produce invalid allocation with nil id
redirect_to setlist_path(#setlist)
else
#FAIL!
render 'new'
end
end
def edit
#songs= Song.search(params[:search])
##songs = Song.all(order: 'title')
#setlist = Setlist.find(params[:id])
#allocations = #setlist.allocations
#allocation = Allocation.new
#selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id ]}
end
def update
#setlist = Setlist.find(params[:id])
#selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id] }
#allocations = #setlist.allocations
#allocation = Allocation.new
#Allocation parameters
#allocation.song_id = params[:allocation][:song_id]
#allocation.setlist_id = #setlist.id
#allocation.songPosition = #setlist.songs.count + 1
if #setlist.update_attributes(params[:setlist])
if #allocation.save
flash[:success] = "SETLIST SAVED!"
redirect_to edit_setlist_path(#setlist)
else
flash[:fail] = "Sorry there was an error adding songs to the setlist"
render 'edit'
end
else
flash[:fail] = "Invalid Set"
render 'edit'
end
end
Any pointers would be much appreciated!

How to test Rspec in controller

In controller,
def admin_search
#admins = User.find(:all,:joins=>[:roles],:conditions=>["name IN (?) and email like '#{params[:email]}%'",["content team","ops team"]]).paginate(:page => params[:page], :per_page => 10)
end
please suggest me some code in rspec
First of all, it's better to extract find(:all, ...) call to User model. Call it search, for instance.
class User < ActiveRecord::Base
scope :search_by_email, lambda { |email|
joins(:roles).where(["name IN (?) and email like '#{email}%'",["content team","ops team"]])
}
end
Use it in the controller then:
def admin_search
#admins = User.search_by_email(params[:email]).paginate(:page => params[:page], :per_page => 10)
end
Now, you can test the search_by_email method in isolation - check, that it returns result for "content team" and "ops team" only, correctly works with empty email string and so on.
I don't think you have to test paginate method, as it should be already tested in kaminari, will_paginate or whatever you use. But if you want to be sure, that it is being called, than you can use mock expectations (should_receive) in the controller specs.
EDIT: How the specs could look like
describe User do
describe ".search_by_email" do
let(:content_team) { Role.create! name: "content team" }
let(:ops_team) { Role.create! name: "ops team" }
let(:another_team) { Role.create! name: "another team" }
it "should search in content team" do
content_team_user = User.create! email: "joe.black#example.com", roles: [content_team]
User.search_by_email("black").should == [content_team_user]
end
it "should search in ops team" do
ops_team_user = User.create! email: "joe.black#example.com", roles: [ops_team]
User.search_by_email("black").should == [ops_team_user]
end
it "should not search in other teams" do
other_team_user = User.create! email: "joe.black#example.com", roles: [another_team]
User.search_by_email("black").should == []
end
it "should not search by empty string" do
content_team_user = User.create! email: "joe.black#example.com", roles: [content_team_user]
User.search_by_email("").should == []
User.search_by_email(nil).should == []
end
# more specs for search...
end
end
describe UsersController do
describe "admin search" do
let(:admin_user) { double(:admin_user).as_null_object }
let(:search_string) { 'joe' }
it "should search for admin users" do
User.should_receive(:search_by_email).with(search_string).and_return([admin_user])
get :admin_search, email: search_string
assigns(:admins).should == [admin_user]
end
end
end

Resources