Skip to content

Simplify the way we explain how to test cookies in controller #2127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2019

Conversation

benoittgt
Copy link
Member

The previous documentation was old and was not clear enough to help
developers to understand properly how to test cookies inside a
controller test. Since then it is much easier to test cookies if you
stick to cookies and don't use response.cookies.

You can use response.cookies when you don't change cookies inside your
test.

def show
  cookies["user_name"] = nil
  head 200
end

RSpec.describe SignOutsController, type: :request do
  describe 'GET /signout' do
    it "clear cookie value" do
      get "/sign_out"

      expect(response.cookies["user_name"]).to eq(nil)
    end
  end
end

Use cookies to set and expect when you set cookies inside your test

def show
  cookies.delete("user_name")
  head 200
end

RSpec.describe SignOutsController, type: :request do
  describe 'GET /signout' do
    it "clear cookie value" do
      cookies["user_name"] = "Sam"

      get "/sign_out"

      expect(cookies["user_name"]).to eq("")
    end
  end
end

This is the preferred way.

Related:

@benoittgt benoittgt changed the base branch from master to 4-0-dev May 13, 2019 20:35
@benoittgt benoittgt force-pushed the update-cookies-cucumber-feature branch 2 times, most recently from 11ac75e to 4d2d408 Compare May 13, 2019 20:39
@benoittgt
Copy link
Member Author

benoittgt commented May 13, 2019

Can we stick rubocop version...? It is failing way too often. But with #2126 it should be ok.

The previous documentation was old and was not clear enough to help
developers to understand properly how to test cookies inside a
controller test. Since then it is much easier to test cookies if you
stick to `cookies` and don't use `response.cookies`.

You can use response.cookies when you don't change cookies inside your
test.

```
def show
  cookies["user_name"] = nil
  head 200
end

RSpec.describe SignOutsController, type: :request do
  describe 'GET /signout' do
    it "clear cookie value" do
      get "/sign_out"

      expect(response.cookies["user_name"]).to eq(nil)
    end
  end
end
```

Use `cookies` to set and expect when you set cookies inside your test

```
def show
  cookies.delete("user_name")
  head 200
end

RSpec.describe SignOutsController, type: :request do
  describe 'GET /signout' do
    it "clear cookie value" do
      cookies["user_name"] = "Sam"

      get "/sign_out"

      expect(cookies["user_name"]).to eq("")
    end
  end
end
```

This is the prefered way and this is way it is documented like that.

Related:
- #1993
@benoittgt benoittgt force-pushed the update-cookies-cucumber-feature branch from 4d2d408 to fa70e86 Compare May 14, 2019 11:54
@JonRowe
Copy link
Member

JonRowe commented May 14, 2019

LGTM, merge on green!

@benoittgt benoittgt merged commit 81d0d60 into 4-0-dev May 14, 2019
@benoittgt benoittgt deleted the update-cookies-cucumber-feature branch May 14, 2019 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants