测试模型交互器 - 更新属性RAILS RSpec

时间:2016-07-27 08:26:36

标签: ruby-on-rails rspec tdd

如何为更新字符串属性编写Rspec测试? 我有属性:" status"和数组= [' First',' Second'] - 模型帖子

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

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

1 个答案:

答案 0 :(得分:0)

看起来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