ruby on rails - RSpec Testing to return Favorited Posts -
i new ruby on rails , enrolled in course @ bloc.io has tasked me creating rspec tests return whether or not post has been favorited specific user.
here current rspec file:
require 'rails_helper' describe user include testfactories describe "#favorited(post)" before @post = associated_post @user = authenticated_user end "returns `nil` if user has not favorited post" expect( @user.favorited(@post) ).to be_nil end "returns appropriate favorite if exists" favorite = favorite.new expect( @user.favorite ).to eq( favorited(@post) ) end "returns `nil` if user has favorited post" end end end i have been successful in passing first test(it "returns 'nil' if user has not favorited post"), stumped on how second , third tests pass.
i think have started second test (it "returns 'nil' if user has not favorited post") correct , believe need supply hash favorite.new assign post , user, unsure need passed in.
here included testfactories file incase helps:
module testfactories def associated_post(options={}) post_options = { title: 'post title', body: 'post bodies must pretty long.', topic: topic.create(name: 'topic name'), user: authenticated_user }.merge(options) post.create(post_options) end def authenticated_user(options={}) user_options = {email: 'email#{rand}@fake.com', password: 'password'}.merge(options) user = user.new(user_options) user.skip_confirmation! user.save user end end as #favorited(post) method:
def favorited(post) favorites.where(post_id: post.id).first end edit:
here favorite model:
class favorite < activerecord::base belongs_to :post belongs_to :user end 2nd edit:
i able change bit of code second test:
it "returns appropriate favorite if exists" favorite = favorite.create(user: @user, post: @post) expect( @user.favorited(@post) ).to eq( favorite ) end i receiving following failure/error:
failures: 1) user#favorited(post) returns appropriate favorite if exists failure/error: expect( @user.favorited(@post) ).to eq( favorite ) expected: #<favorite id: 1, post_id: 1, user_id: nil, created_at: "2015-04-26 04:16:37", updated_at: "2015-04-26 04:16:37"> got: nil (compared using ==) # ./spec/models/user_spec.rb:20:in `block (3 levels) in <top (required)>' finished in 0.53769 seconds (files took 3.21 seconds load) 3 examples, 1 failure, 1 pending failed examples: rspec ./spec/models/user_spec.rb:18 # user#favorited(post) returns appropriate favorite if exists it looks me test having issue finding id specified user. how go locating , assigning specified user's id?
any ideas lead me in right direction?
thanks!
let's have @ second test case:
it "returns appropriate favorite if exists" favorite = favorite.new expect( @user.favorite ).to eq( favorited(@post) ) end firstly, need create favorite, rather new it. using create save database, allow user#favorite find it.
secondly, need associate favorite user , post.
favorite = favorite.create(user: @user, post: @post) (this assumes favorite model has belongs_to associations user , post.)
next, let's @ expectation. might need reload @user model make sure picked it's new favorite.
expect(@user.reload.favorite).to eq(favorited(@post))
Comments
Post a Comment