2010-08-17 65 views
0

我对cucumber和rspec很新,但是有一些rails和ruby的理解。Cucumber Help in Rails 3

有问题的宝石有:设计,Declarative_Authorization和role_model

Rails 3的是什么我使用和Ruby 1.9.2-RC2

记得我不知道我在做什么

我写了一个特点:

Feature: As the administrator of the site give me quick access to information 
    In order to do lots of stuff 
    As an administrator 
    I want to see reports and stuff 

    Background: 
    Given a logged in user with a role of "admin" 

    Scenario: Admin can see links 
    Given I am on the admin dashboard page 
    Then I should see all admin links 

随着一些网站的步骤:

module AdminDashboardHelpers 

    def current_user_session 
    return @user_session if defined?(@user_session) 
    @user_session = user_session 
    end 

    def current_user 
    return @current_user if defined?(@current_user) 
    @current_user = current_user_session && current_user_session.user 
    end 

    def admin_signed_in? 
    @user = current_user 
    @user.has_role?(:admin) 
    end 
end 

World(AdminDashboardHelpers) 


Given /^a logged in user with a role of "([^\"]*)"$/ do |role| 
    visit new_user_session_path 
    fill_in "Login", :with => "iam" 
    fill_in "Password", :with => "secret" 
    click_button "Log in" 
    admin_signed_in? 
    #@current_user.should has_role?(role) 
end 

Then /^I should see all admin links$/ do 
    pending # express the regexp above with the code you wish you had 
end 

我的问题是,当我运行的功能,我收到一个错误说的#Cucumber :: Rails的::全球 未定义的局部变量或方法`USER_SESSION”:0x8077e1f8

我的假设下该user_session是一个设计来的方法。 如何测试用户是否具有某种特定角色?

或者更好的是,我该如何告诉黄瓜有关由宝石提供的rails应用程序的方法? 谢谢你的时间,这是不胜感激。 --iAm

回答

2

通常情况下,你不应该在黄瓜做这种有点测试,它太高级别。你通常会在你的规格中做这些,而不是你的功能。在黄瓜里,你正在浏览一个模拟浏览器,所以你不必设置所有的帮助者等等。只需像访问用户一样访问Web应用程序,然后登录。堆栈应该完成剩下的工作..我正在使用设计,这里是我设置管理员的方式。

Given /^I have one\s+administrator "([^\"]*)" with email "([^\"]*)" and password "([^\"]*)"$/ do |name,email, password| 
    Administrator.new(:email => email, 
        :username=>name, 
        :password => password, 
        :password_confirmation => password).save! 
end 

Given /^I am an Authenticated Administrator$/ do 
    name = 'admin' 
    email = '[email protected]' 
    password = 'secret!' 

    Given %{I have one administrator "#{name}" with email "#{email}" and password "#{password}"} 
    And %{I go to the administrator login page} 
    And %{I fill in "administrator_username" with "#{name}"} 
    And %{I fill in "administrator_password" with "#{password}"} 
    And %{I press "Sign in"} 
end 

Given /^I have not authenticated as an+ "([^"]*)"$/ do |role| 
    visit("/#{role}s/sign_out") 
end 

至于测试某个角色,你可以在rspec中做,而不是在黄瓜。您在黄瓜测试的方式是转到只有该角色可以看到的页面,并确保您可以看到它,然后再进行一次测试,该页面应该无法看到,并确保获得您期待的错误。

+0

谢谢你的回答,我会改变我的方法并继续阅读这个主题。 – creativeKoder 2010-08-18 14:40:11

+0

我的另一个问题是,如果我想要忠于BDD,我应该从一个黄瓜还是rspec测试开始? – creativeKoder 2010-08-18 16:05:07

+1

我推荐阅读Prag Press的Rspec书(http://pragprog.com/titles/achbd/the-rspec-book),但通常你先写功能,编写步骤,使用红色构建视图逻辑:绿色:在Rspec中重构,然后是rspec中的控制器逻辑,然后是对象,然后返回到黄瓜,并希望看到一步传递.. – Doon 2010-08-18 16:23:46