- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在使用 Devise 学习 Rails 3,到目前为止,它似乎运行良好。我有自定义 session 和注册 Controller ,recaptcha 正在运行,登录用户可以通过保存在 S3 上的 carrierwave 上传头像。对我的进步非常满意。
现在我正在编写 Rspec 测试。不太顺利!我有一个合理的用户模型测试,但那是因为我在网上找到它 (https://github.com/RailsApps/rails3-devise-rspec-cucumber/) 并且能够通过遵循 Michael Hartl 的优秀“Ruby on Rails 3 教程”。
我真正的问题是 Controller 测试和集成测试,尤其是 Controller 测试。最初我以为我可以转换 Michael 书中的测试,我必须在很小的程度上进行转换,但进展缓慢,而且我似乎一直在用头撞砖墙——我想,部分原因是我不不太了解 Rspec 和 capybara(犯了一些非常愚蠢的错误),但也因为我对 Devise 的理解不够好,并且想知道 Devise 是否像与 Rspec 一起玩得一样好;我在某处读到,因为 Devise 是基于 Rack 的,所以它可能并不总是像 Rspec 所期望的那样工作。不知道是不是真的?
我知道有些人会想知道为什么这可能是必要的,因为 Devise 是一个 gem,因此已经过测试,但我有几个实例,在我没有立即意识到的情况下,其他地方的更改已经破坏了登录或注册。我认为一套好的 Controller 和集成测试可以解决这个问题。
如果我自己能够做到这一点,我会并且会为其他人发布它,但到目前为止,编写这些测试非常痛苦,我真的需要继续做其他事情。
我敢肯定我不会是唯一一个可以使用它的人。有人知道这样的一套测试吗?
作为对 Jesse 提供帮助的回应...
这是我的 registrations_controller_spec。 “应该呈现‘编辑’页面”中的评论显示了我正在努力解决的问题。此外,“应该创建一个用户”有一些我试图测试但无法测试的东西:
require File.dirname(__FILE__) + '/../spec_helper'
describe Users::RegistrationsController do
include Devise::TestHelpers
fixtures :all
render_views
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
end
describe "POST 'create'" do
describe "failure" do
before(:each) do
@attr = { :email => "", :password => "",
:password_confirmation => "", :display_name => "" }
end
it "should not create a user" do
lambda do
post :create, :user_registration => @attr
end.should_not change(User, :count)
end
it "should render the 'new' page" do
post :create, :user_registration => @attr
response.should render_template('new')
end
end
describe "success" do
before(:each) do
@attr = { :email => "user@example.com",
:password => "foobar01", :password_confirmation => "foobar01", :display_name => "New User" }
end
it "should create a user" do
lambda do
post :create, :user => @attr
response.should redirect_to(root_path)
#response.body.should have_selector('h1', :text => "Sample App")
#response.should have_css('h1', :text => "Sample App")
#flash[:success].should == "A message with a confirmation link has been sent to your email address. Please open the link to activate your account."
#response.should have_content "A message with a confirmation link has been sent to your email address. Please open the link to activate your account."
end.should change(User, :count).by(1)
end
end
end
describe "PUT 'update'" do
before(:each) do
@user = FactoryGirl.create(:user)
@user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
sign_in @user
end
describe "Failure" do
before(:each) do
# The following information is valid except for display_name which is too long (max 20 characters)
@attr = { :email => @user.email, :display_name => "Test", :current_password => @user.password }
end
it "should render the 'edit' page" do
put :update, :id => subject.current_user, :user => @attr
# HAVE PUT THE DEBUGS THAT I'D LIKE TO GET WORKING FIRST
# Would like to be able to debug and check I'm getting the error(s) I'm expecting
puts subject.current_user.errors.messages # doesn't show me the errors
# Would like to be able to debug what html is being returned:
puts page.html # only return the first line of html
# Would like to be able to determine that this test is failing for the right reasons
response.should have_content "Display name is too long (maximum is 20 characters)" # doesn't work
response.should render_template('edit')
end
end
describe "Success" do
it "should change the user's display name" do
@attr = { :email => @user.email, :display_name => "Test", :current_password => @user.password }
put :update, :id => subject.current_user, :user => @attr
subject.current_user.reload
response.should redirect_to(root_path)
subject.current_user.display_name == @attr[:display_name]
end
end
end
describe "authentication of edit/update pages" do
describe "for non-signed-in users" do
before(:each) do
@user = FactoryGirl.create(:user)
end
describe "for non-signed-in users" do
it "should deny access to 'edit'" do
get :edit, :id => @user
response.should redirect_to(new_user_session_path)
end
it "should deny access to 'update'" do
put :update, :id => @user, :user => {}
response.should redirect_to(new_user_session_path)
end
end
end
end
end
最佳答案
根据 Devise Wiki , Controller 测试必须是某种混合(单元+集成)测试。您必须在数据库中创建用户(或您授权实体)的实例。模仿 Devise 的东西真的很难,相信我。
试试这个:How-To:-Controllers-and-Views-tests-with-Rails-3-and-rspec
使用 Cucumber,您可以创建一个已经完成登录过程的步骤。
希望对您有所帮助。
关于ruby-on-rails - 任何人都知道全套的 Devise Rspec/Capybara 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13093403/
我想知道 Rspec 中的命令式和声明式步骤是什么。 这是 Rspec 书中的示例代码: Scenario: transfer money (declarative) Given I have $10
我正在尝试使用 Travis 设置 CI。但是我遇到了在 Travis 上失败但在本地没有的测试,甚至提供了相同的种子。 我认为种子运行相同,但现在我不确定,并想弄清楚它是否存在,所以我现在在哪里看。
RSpec 的文档提到了 --bisect option ,当运行时提供最小的复制,例如 rspec ./spec/calculator_10_spec.rb[1:1] ./spec/calculat
我有一些昂贵的测试设置,仅对我的规范中的少数示例是必需的,如果需要,它只需要运行一次。因为它很慢,所以我试图避免将它放在 before(:each) block 中,但 before(:all) 似乎
在我的 Gemfile 中 gem 'rspec-rails', '~> 3.3' 我发现,errors_on 匹配器移动到了另一个 gem:https://github.com/rspec/rspe
如果我运行此命令“rspec ./spec/requests/api/v1/password_reset_request_spec.rb”,此文件中的所有测试都会通过。 但是,当我运行“rspec”时
我花了 4 天时间尝试正确安装 Ruby on Rails,但我遇到了一个大问题。我第一次使用Linux(对我来说更舒服),但遇到问题后,我使用了Windows。令我惊讶的是,我在 Windows 上
我在这样的验证中测试了很多坏字符串: ['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm',
我的测试套件中有一部分运行着一堆非常慢的导入器。这些测试不需要经常运行(除非实际上正在处理导入器)所以我使用 Rspec 标签将它们分开:http://relishapp.com/rspec/rspe
Rspec 尝试在运行任何规范文件或整个测试套件结束时运行 Test::Unit 测试。它仍然没关系,因为我没有任何测试单元测试文件,但它尝试传递给 rspec 的命令行选项,因为它们将传递给 Tes
我有一段代码要测试: class User :destroy end 请举例测试关联代码的RSpec代码。 最佳答案 对我来说,这个变体效果很好: describe User do it { s
我正在尝试在 rails Controller 规范中重用一些通用代码。我对管理员用户和普通用户有不同的上下文。但是,对于特定操作,大部分行为是相同的,因此我尝试将这种常见行为提取到辅助函数中: de
我有两个类OneClass和 AnotherClass : class OneClass def initialize(*args) @another_member = AnotherCl
我有一个看起来像这样的模型: class Gist def self.create(options) post_response = Faraday.post do |request|
我是 RSPEC 的新手。我编写了一个名为 result_spec.rb 的 RSPEC 代码,如下所示: describe '#grouped_scores' do subject { result
我对字符串类进行了如下扩展: class String def last_character self[-1] end end 我将 string.rb 文件放在 lib 中,如下所示
我是 RSpec 的新手,我在其中编写了一个测试场景: my_object.should_not be_valid 它工作正常。但是,我想测试模型的特定属性是否无效。这样的 RSpec 行为是现成的吗
我正在尝试测试邮政编码属性的长度以确保其长度为 5 个字符。现在我正在测试以确保它不是空白,然后是 4 个字符太短,6 个字符太长。 有没有办法测试它是否正好是 5 个字符?到目前为止,我在网上或 r
我将rspec与电子邮件规范gem一起使用。我正在尝试做: last_delivery = ActionMailer::Base.deliveries.last last_delivery.body.
我创建了一个新的 rails 应用程序,并按照此处的 rspec-rails 安装说明进行操作 - https://github.com/rspec/rspec-rails然后我在我的 app/lib
我是一名优秀的程序员,十分优秀!