(原文连接:http://jjinux.blogspot.com/2009/07/rails-rspec-cucumber-authlogic-and.html)
Friday, July 17, 2009
Rails: RSpec, Cucumber, Authlogic, and factory_girl
After a day of work and a week of reading The RSpec Book, I got RSpec, Cucumber, Authlogic, and factory_girl to play nicely with each other. I can now test user registration, logging in, and logging out.
I can't possibly cover everything from scratch, but I'll cover some of the trickier integration points.
Setup Rails, RSpec, Cucumber, and Authlogic per their own documentation. I ended up with the following in both config/environments/test.rb and config/environments/cucumber.rb:
# There is some duplication between test.rb and cucumber.rb.
config.gem "cucumber", :lib => false, :version => ">=0.3.11"
config.gem "webrat", :lib => false, :version => ">=0.4.4"
config.gem "rspec", :lib => false, :version => ">=1.2.6"
config.gem "rspec-rails", :lib => 'spec/rails', :version => ">=1.2.6"
config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :version => ">=1.2.2", :source => "http://gems.github.com"
I created a factory in test/factories/user.rb:
Factory.define :user do |u|
u.username 'john'
u.email 'john@example.com'
u.password 'funkypass'
u.password_confirmation 'funkypass'
u.first_name 'John'
u.last_name 'Smith'
u.billing_address "1 Cool St.\r\nBerkeley"
u.billing_state 'CA'
u.billing_zipcode '94519'
u.other_zipcode '28311'
u.phone '(925) 555-1212'
end
I created a Cucumber feature in features/user_sessions.feature:
Feature: User Sessions
So that I can blah, blah, blah
As a registered user
I want to log in and log out
Scenario: log in
Given I am a registered user
And I am on the homepage
When I login
Then I should see "Login successful!"
And I should see "Logout"
Scenario: log out
Given I am logged in
And I am on the homepage
When I follow "Logout"
Then I should see "Logout successful!"
And I should see "Register"
And I should see "Log In
To implement this, I created step definitions in features/step_definitions/user_sessions_steps.rb
def user
@user ||= Factory :user
end
def login
user
visit path_to("the homepage")
response.should contain("Log In")
click_link "Log in"
fill_in "Username", :with => "john"
fill_in "Password", :with => "funkypass"
click_button "Login"
response.should contain("Login successful!")
response.should contain("Logout")
end
Given /^I am a registered user$/ do
user
end
When /^I login$/ do
login
end
Given /^I am logged in$/ do
login
end
That's it.
Authlogic's testing documentation talks about stuff like:
require "authlogic/test_case"
...
include Authlogic::TestCase
...
setup :activate_authlogic
UserSession.create(users(:whomever)) # logs a user in
In theory, you should be able to log a user in using some code--without going through the login form. I worked on this for several hours, but I wasn't able to get it to work. I googled quite a bit, but I wasn't able to find anyone else who could get this to work (i.e. integrate RSpec, Cucumber, Authlogic, and factory_girl). It seems that everyone else came to the same conclusion I did--just force the tests to use the login form.
Posted by Shannon -jj Behrens at 11:27 AM
Labels: rails, ruby, testing, web
分享到:
相关推荐
从factory_girl_rails过渡? 查看 。 滑轨 factory_bot_rails提供的Rails集成 。 中列出了受支持的Rails版本。 中列出了受支持的Ruby版本。 下载 GitHub: : 宝石: $ gem install factory_bot_rails 配置 添加...
### Ruby on Rails Guides v2 - Ruby on Rails 4.2.5 #### 一、重要概念及基础假设 - **重要概念**:本指南旨在帮助读者深入理解Ruby on Rails(以下简称Rails)4.2.5版本的核心功能与最佳实践。 - **基础假设**:...
允许您使用为Rails应用程序的前端供电。 是将前端工具像Ruby一样进行编程,纯属喜悦! :smiling_face_with_heart-eyes: 或在运行的检查。 产品特点 :high_voltage: :light_bulb: 即时服务器启动 :high_voltage: ...
factory_girl_api gem是一个Rails引擎,它与集成在一起,以添加用于处理工厂的API路由,这些路由可用于前端测试中的上下文设置。 它与,后者提供了Angular服务,该服务将在测试期间向您的服务器发出正确的请求。 ...
Rspec :: RespectSelectorLimit 确保您CSS文件在旧版IE中不会损坏安装将此行添加到您的应用程序的Gemfile中: gem 'rspec-respect_selector_limit'用法 require 'rails_helper'it 'validates application.css' do ...
工厂女童协会回电 工厂女孩的自定义策略, before关联属性的回调before和after添加。 当属性具有相互依赖的值时很有用。 有关自定义策略的描述,请参阅工厂女孩GETTING_...require Rails.root.join('vendor/factory_
介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...
Rails::API 是 Rails 的精简版本,针对不需要使用完整 Rails 功能的开发者。 Rails::API 移除了 ActionView 和其他一些渲染功能,不关心Web前端的开发者可更容易、快速地开发应用程序,因此运行速度比正常的 Rails ...
持续移动且不破坏事物 :factory: :building_construction: :construction: :construction_worker: 正在建设中-请稍后再回来!
factory_bot-preload 我们都喜欢Rails固定装置,因为它们速度很快,但是我们讨厌处理YAML / CSV / SQL文件。 在此处输入 (FB)。 现在,您可以使用预定义的工厂轻松创建记录。 问题是每次访问数据库以创建记录的...
ruby on rails 相关lib 其中包含:rspec flexmock acts_as_taggable-2.0.2 widgets acts_as_list classic_pagination
##在 Rails 中快速设置 RSpec,使用 Capybara、Factory Girl、Database Cleaner、Shoulda-matchers 等... 在命令行中,运行rails new app_name -T (关闭默认测试单元) 将 Gems 添加到 gemfile,然后捆绑。 gem '...
image_optim_rails 使用image_optim gem优化rails图像资产。 在文件中可以找到获取二进制文件的选项和说明。安装添加到您的Gemfile : gem 'image_optim_rails' 使用 : gem 'image_optim_rails'gem 'image_optim_...
RspecApiHelpers 这是一个宝石,可以帮助您解决所有的Rails API测试难题。 目的是提供使测试API变得轻而易举的功能。 我希望你喜欢它! 将此添加到您的Gemfile中: gem 'rspec_api_helpers'然后执行: $ bundle或将...
- **易于集成**:RSpec 可以轻松地与其他Ruby工具(如Cucumber、Factory Bot等)集成,以支持更复杂的功能测试和数据生成。 #### 知识点三:RSpec 的基本结构 - **描述(Describe)**:用来描述类或模块的行为。 ...
从rails的factory_girl移植到nodejs的端口功能 安装 Node.js: npm install 'factory_girl' 用法 定义工厂 var FactoryGirl = require ( 'factory_girl' ) ; // for nodejs FactoryGirl . define ( 'user' , ...
Ruby on Rails的启动时间助推器,延迟加载整个血腥路由,因此应用程序可以快速启动:sign_of_the_horns:route_lazy_routes route_lazy_routes是一个邪恶的Rails插件,它延迟加载整个血腥路由,直到服务器获得第一个...
rails generate avatars_for_rails:install user 这将生成以下内容: 带有 avatars_for_rails 配置的初始化文件。 提供将 avatars_for_rails 与用户类一起使用的数据库架构的迁移。 不要忘记迁移您的数据库 rake...
jasper-rails-rspec jasper-rails 和 RSpec 集成。 它只是定义了一个名为“包含”的新 RSpec 匹配器。依赖关系 安装 gem install jasper-rails-rspec配置将jasper-rails-rspec添加到您的 Gemfile 中: gem "jasper-...