how testing associated objects?

27 views
Skip to first unread message

Zlodiak Zlodiak

unread,
Sep 1, 2015, 2:40:39 PM9/1/15
to rubyonra...@googlegroups.com
please help solve the problem.

poll controller:

class PollsController < ApplicationController
def index
@user = User.find(params[:user_id])
@polls = @user.polls.paginate(page: params[:page], :per_page =>
10).order(title: :DESC)
end
end

route:

user_polls GET /users/:user_id/polls(.:format)
polls#index

polls_controller_spec.rb:
RSpec.describe PollsController, type: :controller do
describe "GET #index" do
before :all do
@user = FactoryGirl.create(:user)
end


it "assign user as @user" do
get :index, user_id: @user.id
expect(assigns(:user)).to eq(@user)
end


it "assigns polls as @polls" do
get :index, user_id: @user.id
expect(assigns(:polls)).to eq([@poll])
end


it "redirects to the index view" do
get :index, user_id: @user.id
expect(response).to render_template("index")
end
end
end

after run tests, i get follow error message:

kalinin@kalinin ~/rails/phs $ rspec
spec/controllers/polls_controller_spec.rb
.F.
Failures:
1) PollsController GET #index assigns polls as @polls
Failure/Error: expect(assigns(:polls)).to eq([@poll])
expected: [nil]
got: #<ActiveRecord::AssociationRelation []>
(compared using ==)
Diff:
@@ -1,2 +1,2 @@
-[nil]
+[]
# ./spec/controllers/polls_controller_spec.rb:16:in `block (3
levels) in <top (required)>'

Finished in 1.23 seconds (files took 2.81 seconds to load)
3 examples, 1 failure
Failed examples:
rspec ./spec/controllers/polls_controller_spec.rb:14 # PollsController
GET #index assigns polls as @polls


as you can see problem in test 'assigns polls as @polls'. I do not
understand why array @polls is empty

--
Posted via http://www.ruby-forum.com/.

Walter Lee Davis

unread,
Sep 1, 2015, 3:07:54 PM9/1/15
to rubyonra...@googlegroups.com
I think you need to add a factory for polls, and ensure that you assign a poll to that array before you test to see if there are any. Each test runs alone, and not in a predictable order unless you’ve disabled the test randomization feature in Rspec. Each test has a completely new and fresh look at the application, with no preconceived data to work with unless you provide it in your test, as you have done for the @user here.

Walter

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/424c01ce4628e68a91d5de7465cf9604%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Matt Jones

unread,
Sep 2, 2015, 9:26:29 AM9/2/15
to Ruby on Rails: Talk
Unless you accidentally removed it when posting here, you aren't creating any Poll records. `@poll` is also not set to anything...

--Matt Jones 

Stella Pinto

unread,
Sep 10, 2015, 12:24:31 PM9/10/15
to rubyonra...@googlegroups.com
Being from India, this a question to Ruby on rails experts in India... i
have some doubts regarding risk based testing? how to perform it?
Reply all
Reply to author
Forward
0 new messages