0

我有一个失败的 rspec 测试,我似乎无法弄清楚可能出了什么问题。该方法已明确定义,并且适用于网站本身;只是没有通过测试。

 UserPages edit with invalid information
 Failure/Error: visit edit_user_registration_path(user)
 ActionView::Template::Error:
   undefined method `gravatar_for'  for_app_views_devise_registrations_edit_html_erb
 # ./app/views/devise/registrations/edit.html.erb:8:in
 `_app_views_devise_registrations_edit_html_erb
 # ./spec/requests/user_pages_spec.rb:114:in `block (3 levels) in <top (required)>'

这是 rspec 测试:

    describe "edit" do
    let(:user) {FactoryGirl.create(:user)}
    before do
        sign_in user
        visit edit_user_registration_path(user)
    end

    describe "page" do


        it { should have_selector('h1', text: "Update your profile")}
        it { should have_selector('title', text: "Edit user")}
        it { should have_link('change', href: "http://gravatar.com/emails")}
    end

    describe "with invalid information" do
        before { click_button "Save changes"}
        it { should have_content('error')}

这是实际的方法:

 module UsersHelper
#Returns the Gravatar for the given user.
def gravatar_for(user, options = { size: 50})
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    size = options[:size]
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
    end

最后是视图本身的代码:

  <%= gravatar_for @user %>
  <a href="http://gravatar.com/emails" target="_blank">change avatar</a>
4

1 回答 1

0

您的方法是在 中定义的UsersHelper,但是您的视图是针对Devise::Registration. 所以你应该转移到适当的帮助器,应用程序帮助器,或者创建一个具有共享UserDevise::Registration方法的类,并包含在两个帮助器中。

于 2013-06-05T16:03:22.713 回答