2015-01-17 43 views
0

我正试图在rails应用程序上使用ruby来做一些内容测试,以及我使用设计gem来实现用户身份验证。我无法登录到我的应用程序来执行我的测试用例。在rails应用程序中通过水豚身份验证

最初,我的情况如下:

scenario "User arrives at main page" do 
    visit "purchase_orders#index" 
    page.should have_content("All") 
    # some more tests and checks 
end 

凡purchase_orders#指数是通过身份验证的根,其中用户的采购订单中。 但是当我运行测试,我发现了以下错误:

expected to find text "All" in "Log in to manage your orders * Email * Password Forgot your password? Remember me Sign up • Didn't receive confirmation instructions? About Us • Contact Us • 

它告诉我,它没有得到过去的登录页面。接下来我尝试添加以下到我的情况下,在运行测试之前,使其登录:

visit "purchase_orders#index" 
    fill_in('Email', :with => '[email protected]') 
    fill_in('Password', :with => 'password') 
    click_button('Log in') 

其中用户名和密码是实际创建的帐户,但它再次失败,而且不会让过去的标志页。最后,我尝试了以前添加(:每个)方法,如下所示,试图在测试用例签署用户:

before(:each) do 
    visit "users/sessions#new" 
    fill_in('Email', :with => '[email protected]') 
    fill_in('Password', :with => 'password') 
    click_button('Log in') 
end 

这又没有为我工作。所以我的问题是:通过登录页面和实际应用程序的最佳做法和语法是什么?我查找了文档和答案,但没有找到任何东西。

谢谢!

回答

0

找到了答案。我安装了宝石(宝石管理员)和工厂女孩宝石(宝石“factory_girl_rails”,“〜> 4.0”),并安装了捆绑软件。然后,我创建了工厂女孩用户固定在规范文件夹中factory.rb文件如下:

# This is a user factory, to simulate a user object 
FactoryGirl.define do 
    factory :user, class: User do 
     first_name "John" 
     last_name "Doe" 
     email "[email protected]" 
     encrypted_password "password" 
    end 
end 
在我的轨道帮手文件

,我加入这一行能够使用FactoryGirl的方法,而在调用它该类每次:

config.include FactoryGirl::Syntax::Methods 

后来,我加入这行到我的水豚测试页面的顶部:

include Warden::Test::Helpers 
Warden.test_mode! 

最后,我要建T内部使用用户对象他测试的范围:

user = FactoryGirl.build(:user) 

每当我需要登录,我用监狱长的登录方法

 login_as(user, :scope => :user) 

瞧!

相关问题