Ruby on rails Rspec测试失败/错误:预期(当前路径)。到eql(大小路径)
我正在进行Rspec测试。它手动执行测试,一旦单击“创建大小”,它肯定会转到“大小”路径。为什么它将成为根url 错误是Ruby on rails Rspec测试失败/错误:预期(当前路径)。到eql(大小路径),ruby-on-rails,rspec,rspec-rails,Ruby On Rails,Rspec,Rspec Rails,我正在进行Rspec测试。它手动执行测试,一旦单击“创建大小”,它肯定会转到“大小”路径。为什么它将成为根url 错误是 Failure/Error: expect(current_path).to eql(sizes_path) expected: "/sizes" got: "/" (compared using eql?) 测试是 require "rails_helper" require "when_authenticated.
Failure/Error: expect(current_path).to eql(sizes_path)
expected: "/sizes"
got: "/"
(compared using eql?)
测试是
require "rails_helper"
require "when_authenticated.rb"
RSpec.feature "adding size" do
let(:size01) { FactoryGirl.build :size01 }
let(:user) { FactoryGirl.build :user }
let(:admin) { FactoryGirl.create(:user, admin: true, password: "Password18") }
scenario "allow a admin user to add a size" do
admin_logged_in
visit new_size_path
fill_in 'Title', with: 'example'
click_button 'Create Size'
expect(current_path).to eql(sizes_path)
expect(page).to have_content("example")
expect(page).to have_content("You have created a new size")
end
scenario "user can't add size" do
user_logged_in
visit sizes_path
expect(current_path).to eql(root_path)
end
scenario "vistor can't add size" do
visit sizes_path
expect(current_path).to eql(root_path)
end
end
这是我在Sizes controller中的创建方法
def create
@size = Size.new(size_params)
if @size.save
redirect_to sizes_path
flash[:success] = "You have created a new size"
else
render 'new'
end
end
新观点
<center><h1>Create A New Size</h1></center>
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_form_for @size do |f| %>
<%= f.input :title %>
<%= f.button :submit, "Create Size", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
创建新尺寸
由于某些原因,没有正确创建管理员。这是解决办法
require "rails_helper"
RSpec.feature "adding size" do
let(:size01) { FactoryGirl.build :size01 }
let(:user) { FactoryGirl.build :user }
let(:admin) { FactoryGirl.create(:user, admin: true, password: "Password18" ) }
def admin_logged_in
visit login_path
fill_in 'Email', with: admin.email
fill_in 'Password', with: admin.password
click_button 'Log In'
end
def user_logged_in
visit login_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Log In'
end
scenario "allow a admin user to add a size" do
admin_logged_in
visit new_size_path
fill_in 'Title', with: 'example'
click_button('Create Size')
expect(current_path).to eql(sizes_path)
expect(page).to have_content("List Of Sizes")
expect(page).to have_content("You have created a new size")
end
scenario "user can't add size" do
user_logged_in
visit sizes_path
expect(current_path).to eql(root_path)
end
scenario "vistor can't add size" do
visit sizes_path
expect(current_path).to eql(root_path)
end
end
在您的测试中单击_按钮(可能是这一行“expect(current_path).to eql(size_path)”)后是否会发生错误?如果是这样,试着把它拿出来,看看它是否通过。@nersoh是的。单击按钮后,它将重定向到根路径