gpt4 book ai didi

ruby-on-rails - Rails 4 自动生成的删除 RSpec 测试失败,即使该功能正在开发中

转载 作者:行者123 更新时间:2023-12-01 07:48:07 27 4
gpt4 key购买 nike

这是 Rails 4 中为 destroy Action 自动生成的测试,作为 vehicles_controller.rb 规范的一部分:

describe "DELETE destroy" do
it "destroys the requested vehicle" do
vehicle = Vehicle.create! valid_attributes
expect {
delete :destroy, {:id => vehicle.to_param}, valid_session
}.to change(Vehicle, :count).by(-1)
end

it "redirects to the vehicles list" do
vehicle = Vehicle.create! valid_attributes
delete :destroy, {:id => vehicle.to_param}, valid_session
response.should redirect_to(vehicles_url)
end
end

这是我在 Controller 中得到的,同样非常标准:

def destroy
@vehicle = Vehicle.find(params[:id])
@vehicle.destroy
flash[:notice] = "Vehicle has been deleted"
redirect_to vehicles_url
end

这在应用程序本身中工作得很好——当您删除车辆时,它会重定向回 vehicles_url,并且条目会从数据库中删除。服务器日志看起来也完全正常。但是,当我运行规范时,它们失败如下:

1) VehiclesController DELETE destroy destroys the requested vehicle
Failure/Error: expect {
count should have been changed by -1, but was changed by 0
# ./spec/controllers/vehicles_controller_spec.rb:148:in `block (3 levels) in <top (required)>'

2) VehiclesController DELETE destroy redirects to the vehicles list
Failure/Error: response.should redirect_to(vehicles_url)
Expected response to be a redirect to <http://test.host/vehicles> but was a redirect to <http://test.host/>.
Expected "http://test.host/vehicles" to be === "http://test.host/".
# ./spec/controllers/vehicles_controller_spec.rb:156:in `block (3 levels) in <top (required)>'

任何人都可以告诉我这里可能发生了什么导致测试失败吗?感谢您的帮助!

编辑:这里有一些关于可能影响事情的前过滤器的附加信息。在 vehicles_controller 中,因为我使用的是 gem CanCan,所以我在顶部有 load_and_authorize_resource。这个 Controller 也正在测试创建和更新的能力,并且这些规范正在通过,所以我假设这没有干扰,而且它没有失败,没有任何与权限有关的消息。也许我需要更改 Controller 规范顶部的默认 let(:valid_session) { {} } ?我不理会它,因为正如我所说,除了删除之外,它对所有其他操作都没有问题。

进一步编辑:根据下面提供的链接,我将规范编辑为:

describe "DELETE destroy" do
it "destroys the requested vehicle" do
vehicle = Vehicle.create! valid_attributes
expect {
delete :destroy, :id => vehicle.to_param, valid_session
}.to change(Vehicle, :count).by(-1)
end

it "redirects to the vehicles list" do
vehicle = Vehicle.create! valid_attributes
delete :destroy, :id => vehicle.to_param, valid_session
response.should redirect_to(vehicles_url)
end
end

现在,如果我尝试运行规范,我会收到此语法错误:

/home/kathryn/testing/spec/controllers/vehicles_controller_spec.rb:150: 语法错误,意外的 '\n',期待 => (SyntaxError)

第 150 行指的是第一个规范中以 delete :destroy 开头的行,其中进行了更改。

最佳答案

您很可能遇到授权错误。尝试在任一测试中的 delete 操作之后添加 flash[:alert].should == nil。如果您遇到授权问题,您将在测试失败时得到类似 “您无权访问此页面。” 的信息。

最大的线索是您的测试也失败了:

Expected response to be a redirect to <http://test.host/vehicles> but was a
redirect to <http://test.host/>.

授权失败的典型实现重定向到根路径。

我个人不喜欢使用 , valid_session 方法来处理登录。如果您使用的是 Devise,则应使用他们的测试助手并适当登录。例如

describe VehiclesController do
include Devise::TestHelpers

before(:each) do
sign_in FactoryGirl.create(:user)
end

. . .

然后(这很重要!)删除所有 、valid_session 参数,否则它们将覆盖设计为您设置的 session sign_in 方法。

这是最简单的形式;如果某些用户可以(例如)删除而其他用户不能,这仍然会失败,因此对于这些情况,您可能需要创建一个单独的 describe "Admin users can"do ... end 测试 block ,并有一个 before(:each) 调用管理员用户的 sign_in

希望对您有所帮助,祝您好运!

关于ruby-on-rails - Rails 4 自动生成的删除 RSpec 测试失败,即使该功能正在开发中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19123031/

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