Last active
March 2, 2018 01:12
-
-
Save taketo1113/2816f80acaac5d5fda6012a3f9dda4ff to your computer and use it in GitHub Desktop.
Rails Request Spec Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/request_macros.rb | |
module RequestMacros | |
def request_login_user | |
before(:each) do | |
@loginuser = FactoryBot.create(:login_user) | |
post user_session_path, params: { user: { email: @loginuser.email, password: @loginuser.password } } | |
follow_redirect! | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/requests/users_spec.rb | |
require 'rails_helper' | |
RSpec.describe "Users", type: :request do | |
before(:each) do | |
@user = FactoryBot.create(:user) | |
end | |
context "for authenticated users" do | |
request_login_user | |
describe "GET /users" do | |
it "works!" do | |
get users_path | |
expect(response).to have_http_status(200) | |
end | |
end | |
end | |
context "for no authenticated users" do | |
describe "GET /users" do | |
it "redirects to login page" do | |
get users_path | |
expect(response).to redirect_to(new_user_session_path) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment