Ruby on rails Rails教程无法找到字段“0”;名称“;在10.34之后按规格运行
我正在学习Rails教程,我遇到了一个问题,它似乎导致许多其他测试失败。共有23个错误,其中大多数错误说明缺少名为“Name”的字段。当我运行命令时Ruby on rails Rails教程无法找到字段“0”;名称“;在10.34之后按规格运行,ruby-on-rails,ruby,rspec,ruby-on-rails-4,Ruby On Rails,Ruby,Rspec,Ruby On Rails 4,我正在学习Rails教程,我遇到了一个问题,它似乎导致许多其他测试失败。共有23个错误,其中大多数错误说明缺少名为“Name”的字段。当我运行命令时 bundle exec rspec spec/requests/user_pages_spec.rb 我发现以下错误(每次运行时似乎都会改变顺序): 1) 使用有效信息编辑用户页面 失败/错误:在“名称”中填入:新名称 Capybara::ElementNotFound: 找不到字段“Name” #./spec/requests/user\u p
bundle exec rspec spec/requests/user_pages_spec.rb
我发现以下错误(每次运行时似乎都会改变顺序):
1) 使用有效信息编辑用户页面
失败/错误:在“名称”中填入:新名称
Capybara::ElementNotFound:
找不到字段“Name”
#./spec/requests/user\u pages\u spec.rb:136:in'block(4级)in'
结果,许多其他案例似乎都失败了。什么应用程序文件会导致此问题?sessions\u helper.rb
这是我的规范/请求/用户页面\u spec.rb:需要“规范助手”
describe "User pages" do
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all }
it { should have_selector('div.pagination') }
it "should list each user" do
User.paginate(page: 1).each do |user|
expect(page).to have_selector('li', text: user.name)
end
end
end
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
describe "microposts" do
it { should have_content(m1.content) }
it { should have_content(m2.content) }
it { should have_content(user.microposts.count) }
end
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
signup_errors
end
end
describe "with valid information" do
before { valid_signup }
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: 'user@example.com') }
it { should have_link('Sign out') }
it { should have_title(user.name) }
it { should have_welcome_message( 'Welcome')}
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
end
describe "page" do
it { should have_content("Update your profile") }
it { should have_title("Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails') }
end
describe "with valid information" do
let(:new_name) { "New Name" }
let(:new_email) { "new@example.com" }
before do
fill_in "Name", with: new_name
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
click_button "Save changes"
end
it { should have_title(new_name) }
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', href: signout_path) }
specify { expect(user.reload.name).to eq new_name }
specify { expect(user.reload.email).to eq new_email }
end
end
end
运行规范时收到的错误表明
Capybara
需要填写一个名为“Name”
的字段(因为您要求它在行的“Name”中填写:new\u Name
)
如果由于某种原因,登录后您无法访问用户表单,或者用户表单缺少“Name”
字段,这将解释您的失败
在你的简历中,你说你在注册后没有收到任何表格,所以这肯定可以解释这一点
要验证这一点,您可以向描述“页面”
添加测试,如下所示:
it { should have_field("Name") }
如果失败-您不能期望任何依赖于该字段的测试通过…您的HTML表单是什么样子的?是否有一个名称字段?投票决定关闭,但我还没有通读,看它是否是一个合理陈述的问题。它太长了,无法阅读。您的视图中有“名称”字段吗?谢谢大家的快速回复。我知道这是个蹩脚的模糊问题,我不知道该怎么问。当我单击登录时,电子邮件和密码字段都在那里。然而,当我点击注册时,那里根本没有任何表单!我将回顾代码,但有人能建议这样的错误是否会导致表单不显示或visa卡不显示?我不认为这是一个糟糕的问题。当你是初学者时,很难知道哪个代码对问题很重要。