Rails: How to Test an Array's Order with RSpec - ruby-on-rails

I'm new to RSpec so I'm looking for a little help on a simple test:
# controller method
def show
#group = Group.find(params[:id])
#group_members = #group.group_members.order("posts ASC")
end
# in my rspec
it "should show order correctly" do
#group = FactoryGirl.create(:group)
#user_1 = FactoryGirl.create(:user, user_name: "Gary")
#user_2 = FactoryGirl.create(:user, user_name: "Shawn")
#user_3 = FactoryGirl.create(:user, user_name: "Gus")
#user_4 = FactoryGirl.create(:user, user_name: "Jack")
#group_member_1 = FactoryGirl.create(:group_member, group_id: #group.id, user_id: #user_1.id, posts: 30)
#group_member_2 = FactoryGirl.create(:group_member, group_id: #group.id, user_id: #user_2.id, posts: 20)
#group_member_3 = FactoryGirl.create(:group_member, group_id: #group.id, user_id: #user_3.id, posts: 10)
#group_member_4 = FactoryGirl.create(:group_member, group_id: #group.id, user_id: #user_4.id, posts: 15)
visit group_path(#group)
# how do i assert the order of the array?
end
Can someone please help me with a statement to check that the array sorted correctly?

my_array.should eq expected_array
That will make sure that each item is in the exact same spot. If you want to check that an array has the same elements as another array, but the order doesn't matter, do this:
my_array.should =~ expected_array
So in your particular case, you first would need to do a get to the show action, then check the variable. That's done like this:
get :show, :id => #group.id
expected_group_members = [#group_member_3, #group_member_4, #group_member_2, #group_member_1]
assigns(:group_members).should eq expected_group_members
For more information, check out RSpec's GitHub page.

Related

RSpec records attribute doesn't update

Trying to test if making a Vote on an Answer updates its score.
The score updates but its not the same object that gets tested.
( Every Vote has its own voteable Answer object, with same .id)
Therefore the spec fails.
PRY console during rspec testing:
> Answer.first
=> #<Answer:0x000000087b6418 id: 1, (...) score: 2>
> answer
=> #<Answer:0x000000081bea88 id: 1, (...) score: 0>
vote_spec.rb
answer ||= FactoryGirl.create(:answer)
vote = FactoryGirl.create(:vote, like: true, user_id: 1, voteable_id: answer.id, voteable_type: "Answer")
vote2 = FactoryGirl.create(:vote, like: true, user_id: 2, voteable_id: answer.id, voteable_type: "Answer")
vote3 = FactoryGirl.create(:vote, like: true, user_id: 3, voteable_id: answer.id, voteable_type: "Answer")
vote4 = FactoryGirl.create(:vote, user_id: 4, voteable_id: answer.id, voteable_type: "Answer")
expect(answer.score).to eq(2)
vote.rb
after_create :set_voteable_score
private
def set_voteable_score
self.voteable.update_column(:score, count_score(self.voteable))
true
binding.pry
end
def count_score(voteable)
votes = voteable.votes.all
votes.where(like: true).count - votes.where(like: false).count
end
So if I check the vote.voteable.score with all vote(vote, vote2, vote3...), each returns a different voteable object, and the answer in vote_spec.rb doesn't get updated.
Why is this behaviour ( more than 1 record with same id exists)?
If all the voteables have the same id, they're all the same records. What you have then, probably, is different instances of the record, which have been loaded from the db at different times.
Try this:
expect(answer.reload.score).to eq(2)

Rspec testing a controller using from to?

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.

Can't pass spec test because of created_at

I'm testing 'get' method and getting error:
expected:
[{\"user_id\":11,\"user_first_name\":\"bob\",\"user_last_name\":\"marley\",\"user_picture\":\"/images/missing.png\",\"id\":2,\"text\":\"my
third review\",\"date\":\"2013-12-27T09:08:06.364Z\"}]
got:
[{\"user_id\":11,\"user_first_name\":\"bob\",\"user_last_name\":\"marley\",\"user_picture\":\"/images/missing.png\",\"id\":2,\"text\":\"my
third review\",\"date\":\"2013-12-27T09:08:06.000Z\"}]
what the hell? Why last numbers are different while all other fields are equal?
it "should return reviews for user" do
review = Review.create(reviewer_id: #user.id, user_id: #user2.id, text: 'my third review')
get 'get_reviews_for_user', user_id: #user2.id
expect(response).to be_success
json = JSON.parse(response.body)
expect(json['reviews'].to_json).to eq([{user_id: #user.id,
user_first_name: #user.first_name,
user_last_name: #user.last_name,
user_picture: #user.picture.url(:thumb),
id: review.id,
text: review.text,
date: review.created_at
}].to_json)
end
In controller:
def get_reviews_for_user
user = User.where(id: params[:user_id]).first
return render json: {success: false} if user.nil?
reviews = []
Review.where(user_id: user.id).each do |review|
reviewer = review.reviewer
reviews << {user_id: reviewer.id,
user_first_name: reviewer.first_name,
user_last_name: reviewer.last_name,
user_picture: reviewer.picture.url(:thumb),
id: review.id,
text: review.text,
date: review.created_at}
end
render json: { success: true, reviews: reviews }
end
The times are different because the milliseconds are truncated when the time is translated into JSON.
You can use Timecop to freeze the time to a give value like this : Timecop.freeze(Time.now).
You can also use a stub like this (in you test) :
allow(Review).to receive(:where).with(user_id: #user.id).and_return([review])
allow(review).to receive(:created_at).and_return(Time.now)

Rails - how to instantly get id of inserted row

I use this logic in my app:
controller
#current_user = User.find_or_create_from_oauth(auth_hash)
user.rb
def self.find_or_create_from_oauth(auth_hash)
provider = auth_hash["provider"]
uid = auth_hash["uid"].to_s
case provider
when 'twitter'
if user = self.find_by_twitter_uid(uid)
return user
else
return self.create_user_from_twitter(auth_hash)
end
end
end
def self.create_user_from_twitter(auth_hash)
a = self.create({
:twitter_uid => auth_hash["uid"],
:name => auth_hash["info"]["name"]
})
puts a.inspect
user = User.find_by_twitter_uid(a.twitter_uid)
puts '---'
puts user.inspect
end
Immediately after self.create I would need to run this line:
Assignment.create(:user_id => a.id, :role_id => 2)
The problem is, that the line puts user.inspect return something like this:
#<User id: nil, name: "...name...", twitter_uid: "96580821", provider: "twitter", created_at: nil, updated_at: nil>
Why is in the hash returned id: nil?
Or, is there any other way, how to get the ID of last created record?
If the user has been correctly saved, you can use directly a:
a.assignments.create(:role_id => 2)
Otherwise (check using create! instead of create) there may be a validation error.

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