【问题标题】:Rspec test failing miserablyRspec 测试惨遭失败
【发布时间】:2013-04-23 19:19:02
【问题描述】:

所以我在这里看到了几个与我的问题有些相似的问题,但还不够接近,无法帮助我弄清楚我的问题是什么......所以任何关于我做错了什么的帮助将不胜感激:(我我正在关注 Michael Hartl 的教程,因此有关我如何偏离他的示例的信息将特别有帮助)...谢谢

I'm throwing the following 2 errors:

Failures:

  1) Authentication authorization as wrong user submitting a PUT request to the Users#update action 
 Failure/Error: before { put user_path(wrong_user) }
 NoMethodError:
   undefined method `put' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3::Nested_2::Nested_2:0x007f8943728688>
 # ./spec/features/authentication_pages_spec.rb:78:in `block (5 levels) in <top (required)>'

2) Authentication authorization for non-signed-in users in the Users controller submitting  to the update action 
 Failure/Error: before { put user_path(user) }
 NoMethodError:
   undefined method `put' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3::Nested_1::Nested_1::Nested_2:0x007f8944a85ff0>
 # ./spec/features/authentication_pages_spec.rb:61:in `block (6 levels) in <top (required)>'

Finished in 3.06 seconds
56 examples, 2 failures

Failed examples:

rspec ./spec/features/authentication_pages_spec.rb:79 # Authentication authorization as     wrong user submitting a PUT request to the Users#update action 
rspec ./spec/features/authentication_pages_spec.rb:62 # Authentication authorization for non- signed-in users in the Users controller submitting to the update action 

我的代码如下:

spec/features/authentication_pages_spec.rb

require 'spec_helper'

describe "Authentication" do

  subject { page }

  # FOR STATIC SIGN IN PAGE
  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_title('Sign in') }
  end

  # FOR SIGNIN PROCESS
  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_title('Sign in') }
      it { should have_error_message }

      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_error_message }
      end
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user }

      it { should have_title(user.name) }
      it { should have_link('Profile',     href: user_path(user)) }
      it { should have_link('Sign out',    href: signout_path) }
      it { should have_link('Settings',    href: edit_user_path(user)) }
      it { should_not have_link('Sign in', href: signin_path) }

      describe "followed by signout" do
        before { click_link "Sign out" }
        it { should have_link('Sign in') }
      end
    end
  end

  # FOR AUTHORIZING WHICH ACCOUNTS CAN DO WHAT
  describe "authorization" do

    describe "for non-signed-in users" do
      let(:user) { FactoryGirl.create(:user) }

      describe "in the Users controller" do
        describe "visiting the edit page" do
          before { visit edit_user_path(user) }
          it { should have_title('Sign in') }
        end

        describe "submitting to the update action" do
          before { put user_path(user) }
          specify { response.should redirect_to(signin_path) }
        end
      end
    end

    describe "as wrong user" do
      let(:user) { FactoryGirl.create(:user) }
      let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
      before { sign_in user }

      describe "visiting Users#edit page" do
        before { visit edit_user_path(wrong_user) }
        it { should_not have_selector('title', text: full_title('Edit user')) }
      end

      describe "submitting a PUT request to the Users#update action" do
        before { put user_path(wrong_user) }
        specify { response.should redirect_to(root_path) }
      end
    end

  end
end

我的规范/功能/user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  # TESTING STATIC PROFILE PAGE
  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) } # Code to make a user variable
    before { visit user_path(user) }

    it { should have_selector('h1',    text: user.name) }
    it { should have_title(user.name) }
  end

  # TESTING STATIC SIGNUP PAGE
  describe "signup page" do
    before { visit signup_path }

    it { should have_selector('h1',    text: 'Sign up') }
    it { should have_title("Sign up") }
  end

  # TESTING SIGN UP PROCESS
  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 }

        it { should have_title('Sign up') }
        it { should have_content('error') }
      end
    end

    describe "with valid information" do
      before do
        fill_in "Name",         with: "Example User"
        fill_in "Email",        with: "user@example.com"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
      end

      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_title(user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }
        it { should have_link('Sign out') }
      end

    end
  end

  # TESTING EDITING FUNCTIONALITY
  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_selector('h1',    text: "Update your profile") }
      it { should have_title("Edit user") }
      # it { should have_link('change', href: 'http://gravatar.com/emails') }
      # needed if using gravatar
    end

    describe "with invalid information" do
      before { click_button "Save changes" }

      it { should have_content('error') }
    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 { user.reload.name.should  == new_name }
      specify { user.reload.email.should == new_email }
    end

  end

end

我的规范/support/utilities.rb

include ApplicationHelper

RSpec::Matchers.define :have_error_message do |message|
  match do |page|
    page.has_selector?('div.alert.alert-error', text: 'Invalid')
  end
end

def sign_in(user)
  visit signin_path
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
  # Sign in when not using Capybara.
  # cookies[:remember_token] = user.remember_token
end

我的控制器/users_controller.rb

class UsersController < ApplicationController
  before_filter :signed_in_user, only: [:edit, :update]


  def new
    @user = User.new
  end

  def show
     @user = User.find(params[:id])
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to Authentication App..."
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      sign_in @user
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User destroyed"
    redirect_to users_path
  end

  private

    def signed_in_user
      redirect_to signin_url, notice: "Please sign in." unless signed_in?
    end

end

& 最后是我的观点/用户/edit.html.erb

<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirm Password" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
    <% end %>

  </div>
</div>

对于正确修复的任何和所有帮助将不胜感激。谢谢,

【问题讨论】:

    标签: ruby-on-rails rspec tdd railstutorial.org


    【解决方案1】:

    put, get, post, delete 和那些用于控制器测试的方法在功能测试中不可用。

    在编写功能测试时,您应该通过按钮操作/ajax/etc...“提交”put,而不是像控制器测试那样发布到控制器。

    【讨论】:

    • 感谢 kwon - 所以我应该把这些测试放到一个新目录中。或者你能指出我正确的方向吗?抱歉 - 对 rspec 和 tdd 还是很陌生...
    • 也就是说,我可以完全删除描述“提交更新操作”测试,但我认为这不是最好的主意...thx
    【解决方案2】:

    问题是:你把请求 (capybara) 测试和功能测试搞混了

    capybara 中,您应该描述用户步骤

    require 'spec_helper'
    
    describe "Website access" do
      context "when I am a registered user" do
        it "should let me in" do
          page.fill_in 'Email',    with: 'vader@deathstar.com'
          page.fill_in 'Password', with: 'mydearluke'
          page.click_link 'Let me in'
    
          page.should have_content('Welcome, cheif!')
        end
      end
    
      context "when I am not a registered user" do
        it "should not let me in" do
          page.fill_in 'Email',    with: 'jabba.h@tatooine.com'
          page.fill_in 'Password', with: 'wormsarmageddon'
          page.click_link 'Let me in'
    
          page.should have_content('Incorrect credentials!')
        end
      end      
    end
    

    功能测试在较低级别运行,因为您需要使用 HTTP 动词 putgetpostdelete 与您正在测试的应用程序进行通信。

    require 'spec_helper'
    
    describe SessionsController do
      context "when I am registered user" do
        it "should let me in" do
          post :create, email: 'vader@deathstar.com', password: 'mydearluke'
    
          response.should be_success
        end
      end
    
      context "when I am not a registered user" do
        it "should not let me in" do
          post :create, email: 'jabba.h@tatooine.com', password: 'wormsarmageddon'
    
          response.should_not be_success
        end
      end      
    end
    

    这里有几点需要注意:

    1. 我使用的是post,而不是put
    2. 我在 describe 块中指定控制器名称,这与 capybara 测试中的简单英语描述形成对比。
    3. 功能测试指定SessionController 应该如何准确地响应不同的用户凭据(它应该为一个好的用户创建一个会话,并对一个未注册的用户大喊大叫)。

    您要做的是在 request (capybara) 测试中使用 functional 测试中的动词。这是错误的。

    回顾:

    1. capybara 测试中使用visitclick_link/click_button
    2. 在功能测试中使用 HTTP 动词(putgetpostdelete)。
    3. 使用devise gem 进行用户身份验证。

    【讨论】:

    • 我很感激这个人 - 这非常有帮助(显然我没有足够的分数或其他东西来支持你的答案)
    【解决方案3】:

    如果您遵循 Rails 教程,则需要将 Capybara 版本降级到 1.1.2 并将您的规范移动到 spec/requests 目录。更新您的 Gemfile:

    gem 'capybara', '1.1.2'
    

    如果您打算使用 Capybara 2.0,我建议您阅读以下内容:

    http://alindeman.github.io/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

    【讨论】:

    • 谢谢伙计 - 看起来很痛苦......所以基本上我必须回去并将所有不是水豚的测试重新放入规范/请求中???但是将 capybara 测试保存在 /features 文件夹中? (我坚持使用新的 2+ capybara - 该链接非常有帮助)
    • 如果您正在学习 Michael Hartl 的教程,只需将您的文件从 spec/features 移动到 spec/requests。看看 GitHub 上的sample_app
    • 如果其他人有这些特定错误的问题...我能够在这些人的回答的帮助下得到它...只需在规范中重新创建一个重复的 authentication_pages_spec.rb/请求/文件夹...剥离 [describe "submitting to the update action" do] 测试并将其放入新复制的文件中。 (但不要忘记添加 let(:user) { FactoryGirl.create(:user) }
    • 看起来他仍在使用旧版本的水豚...我刚刚在 /features 中进行了所有水豚测试,在 /requests 中进行了其他测试(这似乎正在工作 - 来自当他们被扔进 /requests 时,帖子至少有效
    猜你喜欢
    • 2018-04-25
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多