gpt4 book ai didi

ruby-on-rails - 任何人都知道全套的 Devise Rspec/Capybara 测试

转载 作者:行者123 更新时间:2023-11-28 21:32:53 25 4
gpt4 key购买 nike

我一直在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com