Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise::TestHelpers docs unclear on Devise.mappings[:mapping] usage

In the docs for using the Devise TestHelpers it states to use lines such as...

@request.env["devise.mapping"] = Devise.mappings[:admin]

or

@request.env["devise.mapping"] = Devise.mappings[:user]

...to let Devise know which resource or mapping to use since the controller tests bypass routes.rb. I am not clear on what these mappings are. I have not set up anything special in this regard, and I do not have a specific mapping in mind. I have User, Admin, Refinery, and Superuser roles, a la Rolify, and I am using Cancan for its abilities.

Can you clarify? ...perhaps the resource name is what us meant?

TIA. -Matt

like image 877
Matthew Brown Avatar asked Feb 21 '13 19:02

Matthew Brown


1 Answers

Since waiting for an answer, my research seems to indicate that the mapping indicated in the documentation is actually referring to the resource against which Devise is attached.

Following is speculative on my part, but it appears to work, and it agrees with my scan of the code. For example, if you have only one resource called User you have used Devise for authenticating, you would only need to use the following in your controller spec setup:

@request.env["devise.mapping"] = Devise.mappings[:user]

However, if you have multiple models you are authenticating against, say User and Admin, you would need to use the following, respectively.

@request.env["devise.mapping"] = Devise.mappings[:user]

and

@request.env["devise.mapping"] = Devise.mappings[:admin]

Please feel free to update this question/answer if you have better input.

like image 98
Matthew Brown Avatar answered Oct 29 '22 08:10

Matthew Brown