- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新:请参阅文章末尾了解规范现在如何工作,因为我在规范/请求而不是规范/ Controller 中有我的规范。仍然想知道如何获得有效的登录用户以与我的 Controller 进行集成测试。
我是第一次使用 Devise 和 CanCan,在进行最基本的集成测试时遇到了困难,我正在验证登录用户是否......好吧......已登录。我已阅读处理 Devise 和 RSpec 集成测试的无数帖子和答案(即通过直接通过 https://github.com/plataformatec/devise/wiki/How-To%3a-Test-with-Capybara 、 http://schneems.com/post/15948562424/speed-up-capybara-tests-with-devise 和 Capybara, RSpec and Devise: any way to make integration tests faster by circumventing slow login and setting session directly? 访问 session 来加快它们的速度,但我什至无法获得按预期工作的标准帖子和我很困惑:
由于大量用户遇到类似问题并采用不同的解决方案,显然我不是唯一遇到此问题的人,因为 session 等的各个方面在不同情况下不可用。
精简测试:
subject { page }
describe 'should be able to log in' do
before do
visit '/users/sign_in'
user = FactoryGirl.create(:admin)
#user.add_role :admin
fill_in 'Username', with: user.username
fill_in 'Password', with: user.password
click_on 'Sign in'
end
it { should have_link 'Logout' }
end
...
相关的log.test输出:
Started POST "/users/sign_in" for 127.0.0.1 at 2012-11-06 17:31:10 -0800
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "user"=>{"username"=>"username1", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."username" = 'username1' LIMIT 1[0m
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
[1m[36m (0.1ms)[0m [1mUPDATE "users" SET "last_sign_in_at" = '2012-11-07 01:31:10.722712', "current_sign_in_at" = '2012-11-07 01:31:10.722712', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2012-11- 07 01:31:10.723051' WHERE "users"."id" = 1[0m
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
Redirected to http://www.example.com/
Completed 302 Found in 12ms (ActiveRecord: 0.0ms)
精简了 routes.rb:
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users
精简能力:
类(class)能力 包括 CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
end
...
为了彻底,当前的 Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem "bootstrap-sass", "~> 2.1.0.1"
gem 'faker', '1.1.2'
gem 'will_paginate', '~> 3.0.3'
gem 'bootstrap-will_paginate', '~> 0.0.9'
gem 'jquery-rails'
gem 'client_side_validations'
gem 'thin'
gem "devise", "~> 2.1.2"
gem "cancan", "~> 1.6.8"
gem "rolify", "~> 3.2.0"
gem "simple_form", "~> 2.0.4"
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
group :development, :test do
gem 'sqlite3', '1.3.5'
gem "rspec-rails", "~> 2.11.4"
gem 'guard-rspec', '1.2.1'
#gem 'listen', github: 'guard/listen', branch: 'polling/double'
gem 'guard-spork'
gem 'guard-cucumber'
gem 'spork', '0.9.2'
gem "factory_girl_rails", ">= 4.1.0"
end
group :development do
gem 'annotate', '2.5.0'
gem "quiet_assets", ">= 1.0.1"
end
group :test do
gem 'capybara', ">= 1.1.2"
gem "email_spec", ">= 1.2.1"
gem "cucumber-rails", ">= 1.3.0", :require => false
gem "database_cleaner", ">= 0.9.1"
gem "launchy", ">= 2.1.2"
gem 'rb-fsevent', '0.9.1', :require => false
gem 'growl', '1.0.3'
gem 'terminal-notifier-guard'
end
group :production do
gem 'pg', '0.12.2'
end
还有另一个用户在执行此基本任务时遇到了问题,但事实证明这只是一个语法错误(请参阅 Login integration test with rspec/devise)所以我确定我只是遗漏了一些非常明显的东西。
更新:
你一定是在开玩笑吧。离开它几个小时后,由于某种原因,我发现我在 spec/controllers 中有这个 spec,当简单地将它切换到 spec/requests 时,一切都变绿了。
我引用:
If you are using Rails, put your Capybara specs in spec/requests or spec/integration.
发现于 https://github.com/jnicklas/capybara#readme
但是,我仍然想知道如何针对 future 的需求在需要登录用户的 Controller 上进行集成测试....对于过去的其他非 Devise 应用程序,这一直很简单如下所示:
describe SomeController do
let(:user) { FactoryGirl.create(:user) }
before { valid_sign_in user }
...
我的 helper 在哪里
def valid_sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
uncheck(:remember_me)
click_button "Sign In"
# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token
end
最佳答案
有两个问题在起作用:
<强>1。 rspec/controller 与 rspec/requests
我试图在 spec/controller 下进行集成测试。根据 https://github.com/jnicklas/capybara#readme ,您需要将 Capybara 规范放在 spec/requests 或 spec/integration 中。根据 https://stackoverflow.com/a/5803121/9344 :
A request spec is a thin wrapper around ActionDispatch::IntegrationTest, which doesn't work like controller specs (which wrap ActionController::TestCase). Even though there is a session method available, I don't think it is supported (i.e. it's probably there because a module that gets included for other utilities also includes that method).
最初这可以通过 Devise 通过辅助方法解决,例如:
# file: spec/requests_helper.rb
def login(user)
post_via_redirect user_session_path, 'user[email]' => user.email, 'user[password]' => user.password
end
但是我已经实现的官方/最新解决方案(但请参阅下面第二点了解为什么它没有解决我的问题),可以在 https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara 找到。
<强>2。 Devise::HelperTests 用于 Controller / View 测试,而不是集成测试。
如果我想在 Controller 上做一些测试,我可以使用 Devise::TestHelpers(通过 spec_helper.rb 引用),但即便如此我也无法获得 Capybara 集成测试的功能,因为Devise::TestHelpers 是专门为 Controller / View 测试而不是集成测试编写的。参见 rspec & devise test helpers .
总而言之,我想在 Controller 测试中做的事情无法完成,因为它实际上是一个集成测试,并且 session 等在规范/ Controller 中不可用。但是,有一些 Devise 测试助手可用于帮助进行 Controller / View 测试。
更新
很久以后我重新访问了这个,现在已经让 Warden 助手在我的集成测试中运行。欢呼!这在我的 448 次测试中减少了 8-10 秒。诀窍是 https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara 上的新条目.具体来说:
Capybara-Webkit
If you have trouble using Warden's login_as method with the capybara-webkit driver, try setting run_callbacks to false in the login_as options struct
user = Factory.create(:user)
login_as(user, :scope => :user,
:run_callbacks => false)
总而言之,每次我需要一个经过身份验证的用户进行集成测试时,我不必进行实际的表单填写和发布,现在我只需要 login_as(user, :scope => :user, :run_callbacks => false)
(当然重构为辅助方法)。
关于ruby-on-rails - Capybara、Devise、CanCan 和 RSpec 集成测试 : valid sign in 302 redirects to example. com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262299/
我有一个文件字段,其中包含 opacity: 0并且正在重叠一个假按钮。它是一种常见的 css 技术,可以伪造一种在不同浏览器中一致显示的“上传按钮”。 Capybara 不允许我打电话 attach
我正在尝试为我的 ROR 应用程序编写一些 capybara cucumber 脚本,但发现很难通过每次都进行更改并运行完整功能来使脚本正确。 是否有适用于 Capybara 的 REPL(Read
像这样的html 我的代码是 attach_file("ok","./fileset/publisher/upload_pic.jpg") 但我失败了: 失败: Capybara::Element
我想在我的代码中设置不同的 Capybara 等待时间,具体取决于它们通常需要多长时间才能完全加载?我是否必须重复更改 Capybara.default_wait_time 还是有更好的方法? 最佳答
在 Capybara 中,是否可以在新窗口而不是当前窗口中打开链接? 最佳答案 如果此元素是链接,您只需获取其 href 并在新窗口中打开它: url = find('.some_link')[:hr
对于编辑记录的集成测试,我尝试用新文本替换表单输入字段中的现有文本: find("input[@id='course_title']").set("a safer workplace") 但是,每次我
我有一个动态生成的表单,如下所示: Do you like Pizza? [ ] Yes [ ] No HTML 看起来像这样: Do you like Pizza?
我正在尝试填写两个标有 id="admin_passsword"的文本框。我可以轻松访问第一个文本框,但由于除了占位符和上面的标签之外没有任何区别,我不知道如何访问第二个文本框来填写该字段。
我是一个新手,尝试使用 Capybara 测试我的 Rails 项目,但是当我尝试从我的 div 中检测一些字符串时,我对 page 和 page.body 的含义感到困惑:(in :js=>true
我在使用 rspec、capybara、capybara-webkit 和 timecop 的某些集成规范中看到错误。 Capybara::FrozenInTime: time appears
我使用以下 html 堆栈创建了自定义上传表单: 文件字段通过 display: none 的 css 隐藏属性(property)。因此,当她单击标签(自定义样式)时,用户会调用文件
假设我在 Mac OS X 上安装了 PhantomJS,我可以编写一个普通的旧 Ruby 脚本(没有 Cucumber,没有 RSpec)来驱动 Poltergeist 吗?换句话说,我想要一些 g
我希望单击我的测试应用程序上出现的弹出消息(如果存在)。我对 capybara 很陌生,似乎找不到办法做到这一点。我以前有过使用 watir 的经验,如果我使用 watir 的话,会是这样的: if
我目前使用 Watir-webdriver 进行所有前端测试,但开发团队使用 Capybara 在 Jenkins CI 上运行测试。我们都使用相同的 Cucumber 功能。 值得我们进行两次有效的
Capybara的API似乎不支持配置HTTP代理。有没有什么方法可以和它一起使用? 上下文:我使用 capybara 和 cucumber 来测试 Rails 应用程序,并使用 akephalos
我很好奇是否有更好的方法来测试页面中是否存在完整的 URL(包括协议(protocol))。 使用 Capybara 2.10.2 和 Rails 5.0.0.1,我有以下设置: rails_help
我有以下测试: it 'shows the current quantity of items inside cart' do item = create(:item) visit
我正在使用 Capybara 自动截取一些屏幕截图。 我需要 chrome 浏览器最大化运行,但我似乎无法让它工作。 基于 https://sites.google.com/a/chromium.or
下面是两个单选按钮的 html 代码,它们只是“值”属性不同 我想做的是选择“value =2”的单选按钮 我尝试使用“选择(“AmountOption”)”选择第一个单选按钮,但我想选择第二个单
我尝试使用几种变体来检查 Capybara 的 200 Ok HTTP 响应,但没有一个变体不起作用: response.should be_success page.status.should be
我是一名优秀的程序员,十分优秀!