gpt4 book ai didi

ruby-on-rails - 如何使 RSpec 测试易于管理

转载 作者:行者123 更新时间:2023-12-04 03:52:08 25 4
gpt4 key购买 nike

为了学习 RoR,我开始使用优秀的 Rails Tutorial .到目前为止一切顺利,尽管我注意到 RSpec 测试很快变得一团糟。以下是 sessions_controller.rb 的集成测试示例。随着我继续前进,它只会变得更长。

有没有一种合乎逻辑的方法可以将这些测试分成更小的 block ?你会怎么做?基于什么标准?例子将是最受欢迎的。

例子:

require 'spec_helper'

describe "AuthenticationPages" do
subject { page }

describe "signin" do
before { visit signin_path }

it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: full_title('Sign in')) }

describe "with invalid information" do
before { click_button "Sign in" }

it { should have_selector('title', text: full_title('Sign in')) }
it { should have_selector('div.flash.error', text: 'Invalid') }
it { should_not have_link('Profile', href: signout_path ) }
it { should_not have_link('Settings', href: edit_user_path) }

describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.flash.error') }
end
end

describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end

it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Settings', href: edit_user_path(user)) }
it { should have_link('Users', href: users_path) }
it { should have_link('Sign out', href: signout_path) }

it { should_not have_link('Sign in', href: signin_path) }

describe "visiting the sign up page" do
before { visit sign_up_path }
it { should_not have_selector('h1', text: 'Sign Up') }
it { should_not have_selector('title', text: full_title('Sign Up')) }
end

describe "submitting to the create action" do
before { post users_path(user) }
specify { response.should redirect_to(user_path(user)) }
end

describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end

describe "authorization" do

describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }

describe "in the users controller" do

describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_selector('title', text: 'Sign in') }
end

describe "submitting to the update action" do
before { put user_path(user) }
specify { response.should redirect_to(signin_path) }
end
end

describe "visiting user index" do
before { visit users_path }
it { should have_selector('title', text: 'Sign in') }
end

describe "when attempting to visit a protected page" do
before do
visit edit_user_path(user)
sign_in user
end

describe "after signing in" do
it "should render the desired protected page" do
page.should have_selector('title', text: 'Edit user')
end

describe "when signing in again" do
before do
visit signin_path
sign_in user
end

it "should render the default (profile) page" do
page.should have_selector('title', text: user.name)
end
end
end
end
end

describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user }

describe "visiting users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should have_selector('title', text: 'Sample App') }
end

describe "submitting a PUT request to the users#update action" do
before { put user_path(wrong_user) }
specify { response.should redirect_to(root_path) }
end
end

describe "as non-admin user" do
let(:user) { FactoryGirl.create(:user) }
let(:non_admin) { FactoryGirl.create(:user) }

before { sign_in non_admin }

describe "submitting a DELETE request to the Users#destroy action" do
before { delete user_path(user) }
specify { response.should redirect_to(root_path) }
end
end
end
end

最佳答案

好吧,既然您已经将 RSpec 与 shoulda 一起使用(对吗?),我认为您已经实现了高水平的可读性和可管理性。您总是可以将此规范拆分为更小的部分,但您必须问自己是否真的需要为一个 Controller 拆分测试代码?你有很多describe擅长结构化测试的部分。如果有任何失败,RSpec 将始终为您提供准确的行号,以便您可以直接进入并修复它。

至于额外的可读性,我注意到您在 describe 之后使用了空行。部分。有些人还喜欢在 end 之前插入空行。陈述。我还建议写下你以 end 结尾的 block 。声明,像这样:

describe "GET /posts" do
#[...]
end # GET /posts

有了这种结构的部分,许多编辑器中还有一个很好的功能,它允许通过隐藏代码并显示 end 来缩小块内的代码。就在 describe 之后.我相信你会自己解决这个问题。我从没想过额外的可读性或任何超出基础的东西,我可以很好地管理我编写的测试。

希望这能让您相信您已经拥有了一种组织代码的好方法。我不认为针对相同功能/对象/目标的拆分测试只是为了将其保持在 < 100 之下。线左右。

更新

我最近读了一篇 article其中 DHH 指出 RSpec 是不必要的复杂,并且 test/unit可读且易于维护。我想你可能想知道。

关于ruby-on-rails - 如何使 RSpec 测试易于管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9419084/

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