开发者

How to get the authentication status in clearance for tests

开发者 https://www.devze.com 2023-02-13 02:06 出处:网络
The first rails app I wrote used the hand-rolled authentication from railstutorial. For my second app I\'m using Clearance.I\'m trying to write the integration tests for whether clicking the sign in

The first rails app I wrote used the hand-rolled authentication from railstutorial.

For my second app I'm using Clearance. I'm trying to write the integration tests for whether clicking the sign in/sign out links have worked. In the railstutorial version I can use the following:

describe "success" do
  it "should sign a user in and out" do
    user = Factory(:user)
    visit signin_path
    fill_in :email,    :with => user.email
    fill_in :password, :with => user.password
    click_button
    controller.should be_signed_in
    click_link "Sign开发者_运维技巧 out"
    controller.should_not be_signed_in
  end
end

How do I do the equivalent thing with clearance? I just can't work it out. It seems to be telling me that controller is nil.


I did the EXACT same thing, read Rails Tutorial and then started my own app using Clearance. Users seemed like the right starting point, so that's where I started. I hadn't even replaced the default static home page that displays Rails links and such. I believe that was actually the problem. Clearance doesn't define user profile pages or anything, so when you sign in it redirects you to the home page (unless you were on your way somewhere else but needed to sign in). The default home page is just a static file, so it's not running through the typical Rails request cycle and doesn't load any of the controllers.

I ended up just adding a super simple Pages controller (like in the Rails Tutorial, chapter 3) so it would redirect to dynamic page after signing in. This made the test above almost pass. The last edit I needed to get things working was

click_link "Sign Out", :method => :delete

Here, I specified that click_link should be using the :delete method instead of :get, which it aparently does by default. I'm still rather new to Rails, barely finished the book a few days ago, so I'm not sure why just click_link worked in the book but not in my app (maybe the move from Rails 3.0 to 3.1?), but this got everything passing.

0

精彩评论

暂无评论...
验证码 换一张
取 消