gpt4 book ai didi

ruby-on-rails - 编写 rspec/capybara 测试的正确方法?

转载 作者:行者123 更新时间:2023-11-28 20:56:09 25 4
gpt4 key购买 nike

我是 Ruby 和 Rails 的新手,我正在尝试以正确的方式实现测试。我目前正在努力让用户注册。我希望第一个用户注册成为管理员,之后的每个用户都成为普通用户。

我目前认为我需要将我的测试编写为一个功能,但我想知道这是否真的应该是一个模型测试。

我当前的代码就在这里

require 'spec_helper'

describe "User pages" do
subject { page }

describe "Sign up page" do
before { visit signup_path }

it { should have_button('Create!') }
end

describe "Creating an account" do
before { visit signup_path }
let(:submit) { "Create!" }

describe "with invalid information" do
it "should not create the user account" do
expect { click_button "Create!"}.not_to change(User, :count)
end

describe "it should display errors" do
before { click_button submit }

it { should have_content('Failed signup') }
end
end

describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "password"
fill_in "Confirm Password", with: "password"
end

it "should create the user" do
expect { click_button submit }.to change(User, :count).by(1)
end

# User sign in not implemented at this point
describe "the first user" do
User.all.count.should == 0
click_button submit
User.all.count.should == 1
@firstUser = User.first
@firstUser.is_admin?.should == true

describe "the second user" do
before do
visit signup_path
fill_in "Name", with: "Example User2"
fill_in "Email", with: "user2@example.com"
fill_in "Password", with: "password"
fill_in "Confirm Password", with: "password"
end

click_button submit
@secondUser = User.all.last
@secondUser.is_admin?.should == false
end
end
end
end

结束

我最关心的是“有有效信息”的部分,我想把它清理干净,让它正确地适应 rspec/capybara。

最佳答案

你可以试试这个很棒的tutorial作者 michael hartl,他在本教程中使用了 Rspec,您可以从中学习 :)我也是初学者,我从它开始:D

关于ruby-on-rails - 编写 rspec/capybara 测试的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17606219/

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