Rspec测试预期:“/ sizes”得到:“/”

时间:2015-09-10 11:51:19

标签: ruby-on-rails rspec

我有一个试图运行的Rspec测试。它手动完成测试,一旦你点击Create Size,它肯定会转到sizes_path。为什么要使用root url?

错误是

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控制器中的Create Method

  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>

1 个答案:

答案 0 :(得分:0)

问题在于我的管理员

以下是总计的修复

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" ) }

    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