Skip to content

Instantly share code, notes, and snippets.

@unixc3t
Last active May 1, 2017 07:57
Show Gist options
  • Save unixc3t/3b69f6df9f900b3ba46e19bd0c9373b3 to your computer and use it in GitHub Desktop.
Save unixc3t/3b69f6df9f900b3ba46e19bd0c9373b3 to your computer and use it in GitHub Desktop.
rspec code snippet
# test get /
it 'get index' do
expect(get: "/").to route_to(controller: "links",
action: "index")
end
it 'get the about page' do
get :about
p response
expect(response).to render_template("about")
end
# devise login and use login user
let(:user) do
create(:user)
end
let(:login) do
@request.env["devise.mapping"] = Devise.mappings[:user]
sign_in user
end
it 'locates the requested @link' do
login
get :edit, id: link
expect(assigns(:link)).to eq link
expect(assigns(:link).user_id).to eq user.id
end
FactoryGirl.define do
factory :user do
email {FFaker::Internet.email}
password '123456'
password_confirmation '123456'
end
end
@unixc3t
Copy link
Author

unixc3t commented Apr 28, 2017

FactoryGirl.define do
factory :recipe do
title FFaker::Book.title
description FFaker::Book.description
image { Rack::Test::UploadedFile.new(Rails.root.join('spec', 'photos', 'test.png'), 'image/png') }
user_id 1
end
end

@unixc3t
Copy link
Author

unixc3t commented May 1, 2017

it 'request upvote the link' do
login
request.env["HTTP_REFERER"] = "where_i_came_from"
patch :upvote, id: link
expect(assigns(:link).get_upvotes[0].vote_weight).to eq 1
expect(assigns(:link).get_upvotes[0].voter_id).to eq user.id

  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment