如何获取测试许可中的身份验证状态

发布于 2024-10-19 15:38:17 字数 601 浏览 2 评论 0原文

我编写的第一个 Rails 应用程序使用了 railstutorial 中的手动身份验证。

对于我的第二个应用程序,我使用的是 Clearance。我正在尝试编写集成测试来确定单击登录/注销链接是否有效。在 Railstutorial 版本中,我可以使用以下内容:

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

如何通过间隙做等效的事情?我就是搞不出来。它似乎告诉我控制器为零。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2024-10-26 15:38:17

我做了完全相同的事情,阅读了 Rails 教程,然后使用 Clearance 启动了我自己的应用程序。用户似乎是正确的起点,所以这就是我的起点。我什至没有替换显示 Rails 链接等的默认静态主页。我相信这实际上就是问题所在。 Clearance 不定义用户个人资料页面或任何内容,因此当您登录时,它会将您重定向到主页(除非您正在其他地方但需要登录)。默认主页只是一个静态文件,因此它不会运行典型的 Rails 请求周期,也不会加载任何控制器。

我最终只是添加了一个超级简单的 Pages 控制器(就像 Rails 教程第 3 章中的那样),这样它就会在登录后重定向到动态页面。这使得上面的测试几乎 通过。我需要进行的最后一次编辑是在

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

这里,我指定 click_link 应该使用 :delete 方法而不是 :get ,它显然默认情况下会这样做。我对 Rails 还很陌生,几天前才刚刚读完这本书,所以我不确定为什么只是 click_link 在书中起作用,但在我的应用程序中不起作用(可能是从 Rails 3.0 迁移而来)到 3.1?),但这让一切都过去了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文