【问题标题】:Testing model interactor - update attribute RAILS RSpec测试模型交互器 - 更新属性 RAILS RSpec
【发布时间】:2016-07-27 08:26:35
【问题描述】:

如何为更新字符串属性编写 Rspec 测试? 我有属性:“状态”和数组 = ['First','Second'] - model Post

it "update status attribute" do expect(@result).to change(Post, :status) end

对吗?

我的交互者:

class User::Post::RequestVerification <  ActiveInteraction::Base

  object :user

  string :status, default: nil
  integer :id, default: nil

  validate :status

  def execute
    update_status!
  end

  def post
    @post ||= post.find(id)
  end

  def update_status!
    @post.update_atrributes!(
        status: ['First', 'Second']
    )
  end

  def status
    errors.add(:base, 'error')
  end
end

我的 RspecTest:

require 'rails_helper'

describe User::Post::RequestVerification do

  before do
    @result = User::Post::RequestVerification.run(new_params[:post])
  end

  let!(:user) {Post.new}
  let!(:user) { User.create }
  let!(:new_params) do
    {
        post: {

            status: 'First'

        }
    }
  end


  it "should update status" do
    expect(@result).to change(Post, :status)
  end

    end

我想写一个测试来更新一个属性。

【问题讨论】:

  • 你运行了吗?您看到什么错误或意外输出?
  • expected #status to have changed, but was not given a block 和 binding.pry @details={:base=&gt;[{:error=&gt;"error"}, {:error=&gt;"error"}], :status=&gt;[{:type=&gt;"string", :error=&gt;:invalid_type}]}, @messages={:base=&gt;["error", "error"], :status=&gt;["is not a valid string"]}&gt;

标签: ruby-on-rails rspec tdd


【解决方案1】:

看起来change 的语法不同。您提到的类似错误也有类似的问题。我不会复制答案,我可以在那里推荐你。

expected #count to have changed by 1, but was not given a block

另外,您似乎需要将changebyfrom(..).to(..) 链接起来

查看文档https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/expect-change

【讨论】:

  • 谢谢,但这是为更新属性编写测试的正确方法(使用 rspec 匹配器“更改”)?
  • 看起来方法不对!请查看我在答案中发布的文档。我更新了。
  • expect { @result }.to change(@result, :status).from('First').to('Second')你怎么看?
  • 阅读这个问题的答案:stackoverflow.com/questions/25227196/…
  • 但“改变”只属于数字,即 from(1).to(2) ??我不需要增加计数,而是更改字符串值。可以使用 change to 吗?
猜你喜欢
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多