为什么这个 rspec 请求规范不更新模型?

2024-05-26

我有一个与用户模型交互的请求规范。我想确保具有管理员角色的用户可以创建/编辑/销毁用户。我现在遇到一个问题,编辑操作无法更新用户。当我手动执行网站本身的操作时,一切正常,但测试无法更新用户。

这是我的规格:

it 'edits a user' do
  @user = FactoryGirl.create(:user)
  visit new_user_session_path unless current_path == new_user_session_path
  fill_in "Email", :with => @user.email
  fill_in "Password", :with => @user.password
  click_button "Sign In"
  user_to_edit = FactoryGirl.create(:user, first_name: "John", last_name: "Smith")
  visit edit_user_path(user_to_edit) unless current_path == edit_user_path(user_to_edit)
  fill_in 'user_last_name', with: "Changed"
  expect{
    click_button "Do it"
  }.to change { user_to_edit.last_name }.from("Smith").to("Changed")
  page.should have_content "John Changed"
end

我得到的错误是:

Failure/Error: expect{
       result should have been changed to "Changed", but is now "Smith"

如果我将测试的最后几行更改为:

  fill_in 'user_last_name', with: "Changed"
  click_button "Do it"
  page.should have_content "John Changed"

然后测试成功。这似乎不对,因为如果满足以下条件,页面不应显示“John Changed”:user_to_edit没有更新。

我的删除请求规范工作正常:

it "deletes a user" do
  @user = FactoryGirl.create(:user)
  visit new_user_session_path unless current_path == new_user_session_path
  fill_in "Email", :with => @user.email
  fill_in "Password", :with => @user.password
  click_button "Sign In"
  user_to_delete = FactoryGirl.create(:user, first_name: "John", last_name: "Smith")
  visit users_path unless current_path == users_path
  expect{
    within ".user_#{user_to_delete.id}" do
      click_link 'Delete'
    end
  }.to change(User,:count).by(-1)
  page.should_not have_content "John Smith"
end

我有一个用户模型:

class User < ActiveRecord::Base
  ROLES = %w[renter landlord admin]
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation :first_name, :last_name, :role

  validates :password, :presence => true, :on => :create
  validates :first_name, :presence => true
  validates :last_name, :presence => true

  before_save :set_phones

  def set_phones
    self.fax = Phoner::Phone.parse(self.fax).format("%a%n") unless self.fax.blank?
    self.land_phone = Phoner::Phone.parse(self.land_phone).format("%a%n") unless land_phone.blank?
    self.mobile_phone = Phoner::Phone.parse(self.mobile_phone).format("%a%n") unless mobile_phone.blank?
  end
end

我有这个工厂:

require 'faker'

FactoryGirl.define do
  factory :user do |f|
    f.first_name { Faker::Name.first_name }
    f.last_name { Faker::Name.last_name }
    f.email {Faker::Internet.email}
    f.password { "oq2847hrowihgfoigq278o4r7qgo4" }
    f.role { "admin" }
  end
end

我的用户控制器中有这些操作:

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

    respond_to do |format|
      format.html
    end
  end

  def update
    if params[:user][:password].blank?
      [:password,:password_confirmation].collect{|p| params[:user].delete(p) }
    end

    respond_to do |format|
      if @user.errors[:base].empty? and @user.update_attributes(params[:user])
        flash.now[:notice] = "Your account has been updated"
        format.html { render :action => :show }
      else
        format.html { render :action => :edit, :status => :unprocessable_entity }
      end
    end
  end

The routes.rb文件也相关,因为我正在使用 Devise 并有一个自定义用户控制器:

  devise_for :users, :skip => [:sessions, :registrations]

  devise_scope :user do
    get "login" => "devise/sessions#new", :as => :new_user_session
    post 'login' => 'devise/sessions#create', :as => :user_session
    delete "logout" => "devise/sessions#destroy", :as => :destroy_user_session
    get "signup" => "devise/registrations#new", :as => :new_user_registration
    put "update-registration" => "devise/registrations#update", :as => :update_user_registration
    delete "delete-registration" => "devise/registrations#destroy", :as => :delete_user_registration
    get "edit-registration" => "devise/registrations#edit", :as => :edit_user_registration
    get "cancel-registration" => "devise/registrations#cancel", :as => :cancel_user_registration
    post "create-registration" => "devise/registrations#create", :as => :user_registration
  end

  resources :users, :controller => "users"

你被智能测试框架的外观所愚弄了:) 你肯定期望db entry for user_to_edit改变。user_to_edit是一个局部变量,所以user_to_edit.last_name无论您单击什么按钮都不会改变。尝试与{ user_to_edit.reload.last_name }

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么这个 rspec 请求规范不更新模型? 的相关文章

随机推荐

  • android 中的自定义 MediaPlayer 类

    我刚刚完成了一个项目 我使用网络视图进行视频播放 现在我被要求用自定义播放器替换 webview 部分 玩家应该能够处理 HLS 如果我使用VideoView和MediaController 我可以播放直播 但不幸的是 MediaContr
  • 选择列的一部分

    我想知道是否有人可以帮助查询以选择列的一部分 criteriadata 列包含如下所示的数据 标准数据 14 27 15 14 30 15 DD 14 38 15 通过 14 33 15 通过 如何只选择数字 15 之后出现的数据 非常感谢
  • AVAudioPlayer 在 iPhone 5 中无法工作

    我正在开发一个 iPhone 应用程序 在这个应用程序中 我使用 AVAudioPlayer 来播放音频 这是我播放声音的代码 NSString resourcePath NSBundle mainBundle resourcePath r
  • 如何为 VueJS 组件创建 v-model 修饰符?

    在VueJS中有一些v model修改预解析绑定值 例如v model trim从字符串中删除空格 如何创建自己的修改器 例如v model myparse今天嗯使用类似的东西 computed name get function retu
  • 避免在 Flutter Web 插件包之外使用纯 Web 库

    我正在构建一个 Flutter 应用程序 并尝试在网络上运行 其中一部分包含一些特定于网络的代码 import dart html as html import package flutter foundation dart class D
  • to_proc 方法在 Ruby 中意味着什么?

    我正在学习 Rails 并关注这个线程 https stackoverflow com questions 1961030 我被困住了to proc方法 我认为符号只是字符串的替代品 它们就像字符串 但在内存方面更便宜 如果我还缺少任何其他
  • 在 Cordova 中单击 html 按钮时启动新活动

    我正在尝试使用 Cordova 和 Java 构建 Android 应用程序 我想构建当在 HTML 代码中单击按钮时启动新 Intent 的意图 这是我的 HTML 代码 h2 Hello Android h2
  • Aptana Python stdlib 与 virtualenv 的问题

    我最近开始开发一个项目 仅使用 vim 作为我的文本编辑器和 virtualenv 设置 我从 GitHub 在此 virtualenv 上安装了一些 API 最终 项目变得有点大了 超出了 vim 的处理能力 所以我不得不将项目移动到 I
  • iOS 5 核心数据冻结

    我尝试做以下简单的事情 NSArray entities context executeFetchRequest inFetchRequest error fetchError 没有什么花哨 但这在 iOS 5 中冻结 在 iOS 4 中工
  • Kubeadm 加入失败:无法请求集群信息

    我有两台服务器作为本地服务器网络上的主节点和工作节点 master node 10 20 20 214 worker node 10 20 20 218 在主节点中 我成功使用 kubeadm init 设置 Calico 网络 它报告消息
  • 如何清除组合框?

    我有一些组合框设置为下拉列表 用户可以在其中选择一个数字 我还有一个清除按钮 应该清除组合框中的文本 但我似乎无法得到它 我试过了 doesn t work cboxHour Text and doesn t work cboxHour R
  • Maven 快速入门指南 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我的任务是为工作中的新程序设置开发环境 领导者选择 Eclipse 作为其 OSGi 支持的 IDE 并选择 Maven 作为构建实用程序
  • 如何在 Rails 中格式化日期时间?

    在我的 Rails 视图中 我有以下显示日期时间的代码 该行的结果如下 2013 02 02 01 00 00 UTC 我该如何更改它 使其显示为 2 2 13 X 00 PST 使用红宝石的strftime 关于日期 日期时间 看一下文档
  • Kafka Streams - 如何扩展 Kafka 存储生成的变更日志主题

    我有多个冗余应用程序实例 它们想要使用主题的所有事件并独立存储它们以进行磁盘查找 通过rocksdb 为了便于论证 我们假设这些冗余消费者正在服务无状态 http 请求 因此 负载不是使用 kafka 共享的 而是使用 kafka 将数据从
  • DirectX::SpriteFont/SpriteBatch 阻止 3D 场景绘制

    我使用时遇到问题DirectX SpriteFont DirectX SpriteBatch 来自 DirectXTK 与此处讨论的问题完全相同 使用 SpriteFont 类绘制文本时出现问题 https stackoverflow co
  • 是否可以仅使用一个实例来创建片段

    我只是想知道 片段创建只能有一个实例或单例吗 我经历了谷歌iosched项目也 他们只是简单地创造 Fragment a new Fragment 每当他们想要 假设例如 public static FragmentManager inst
  • 从文件中读取行,处理它,然后将其删除

    我有一个 22mb 的文本文件 其中包含数字列表 每行 1 个数字 我试图让 python 读取数字 处理数字并将结果写入另一个文件中 所有这些都有效 但如果我必须停止该程序 它就会从头开始 我一开始尝试使用mysql数据库 但它太慢了 我
  • 使用 PIP 从 Github 安装 Python 包

    我已经看到文档表明您可以通过以下方式使用 pip 安装托管 Python 包的 Github sudo pip install e git git github com myuser myproject git egg myproject
  • 水晶报表参数选择有限制吗?

    我正在尝试根据按用户分组的 Oracle 数据库制作 Crystal Reports 11 报告 我有超过一千个用户 我想创建一个参数字段 提示用户选择他们想要查看其结果的用户 但是我的参数选择字段仅显示 221 个可能的用户 由于 SQL
  • 为什么这个 rspec 请求规范不更新模型?

    我有一个与用户模型交互的请求规范 我想确保具有管理员角色的用户可以创建 编辑 销毁用户 我现在遇到一个问题 编辑操作无法更新用户 当我手动执行网站本身的操作时 一切正常 但测试无法更新用户 这是我的规格 it edits a user do