Ruby On Rails教程(代码清单9.43)

时间:2014-02-21 12:33:00

标签: ruby-on-rails ruby

我正在关注Michael Hartl的Ruby on Rails教程。我在第9章清单9.43中,问题是我无法显示删除链接,而且我的两个测试都失败了

>Failures:
>
> 1) User pages delete links as an admin user 
>     Failure/Error: it { should have_link('delete', href: user_path(User.first)) }
>      expected #has_link?("delete", {:href=>"/users/211"}) to return true, got false
>     # ./spec/requests/user_pages_spec.rb:43:in `block (4 levels) in <top (required)>'
>
>  2) User pages delete links as an admin user should be able to delete another user
>     Failure/Error: click_link('delete', match: :first)
>     Capybara::ElementNotFound:
>       Unable to find link "delete"
>     # ./spec/requests/user_pages_spec.rb:46:in `block (5 levels) in <top (required)>'
>     # ./spec/requests/user_pages_spec.rb:45:in `block (4 levels) in <top (required)>'
>
>Finished in 12.59 seconds
>73 examples, 2 failures
>
>Failed examples:
>
>rspec ./spec/requests/user_pages_spec.rb:43 # User pages delete links as an admin user 
>rspec ./spec/requests/user_pages_spec.rb:44 # User pages delete links as an admin user >should be able to delete another user

这是我的_user.html.eb文件

<li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
  <% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                                  data: { confirm: "You sure?" } %>
  <% end %>
</li>

这是我的factories.rb文件

FactoryGirl.define do
  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end
end

这是我的sample_data.rake

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
   admin= User.create!(name: "Example User",
                 email: "example@railstutorial.org",
                 password: "foobar",
                 password_confirmation: "foobar",
                 admin:true)
    99.times do |n|
      name  = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password  = "password"
      User.create!(name: name,
                   email: email,
                   password: password,
                   password_confirmation: password)
    end
  end
end

但我确信问题在于,sessions_helper.rb

module SessionsHelper
    def sign_in(user)
        remember_token = User.new_remember_token
        cookies.permanent[:remember_token] = remember_token
        user.update_attribute(:remember_token, User.encrypt(remember_token))
        self.current_user = user
    end
    def signed_in?
        !current_user.nil?
    end

    def current_user=(user)
    @current_user = user
      end

      def current_user?(user)
      user == current_user
    end

    def current_user
        remember_token = User.encrypt(cookies[:remember_token])
        @current_user ||= User.find_by(remember_token: remember_token)
    end

这是我的user_pages_specs       描述“删除链接”做

  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

1 个答案:

答案 0 :(得分:0)

确保在您检查delete link时以admin身份登录。 此外,您的spec/requests/user_pages_spec.rb文件中还包含以下描述块。

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           ##### Sign in as admin before checking delete link
      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