PUT #update控制器测试失败,因为它没有修改给定的对象

时间:2019-03-28 12:27:23

标签: ruby-on-rails ruby rspec

我正在尝试使用rspec在rails api中测试我的更新操作,但是我编写的测试套件似乎无法使用提供的参数来更新对象。

在我的api中,我有两个模型:氏族和武士,其中武士属于氏族,氏族有很多武士。 我使用factory bot生成了固定装置,并且Clan固定装置在外部范围中定义(因此clan.id)。

# ../samurais_controller_spec.rb

describe "PUT #update" do
    context "with valid attributes" do
      let(:new_attributes) { { name: "Danny Boy" } }
      let(:new_samurai) { create(:samurai, name: "Danny", clan_id: clan.id) }

      it "updates samurai" do
        put :update, params: { clan_id: new_samurai.clan_id,
                               id:      new_samurai.id,
                               samurai: new_attributes }
        new_samurai.reload
        expect(new_samurai.name).to eq(new_attributes[:name])
      end
    end
  end
# ../samurais_controller.rb

def update
      samurai.update!(samurai_params)

      render json: samurai.to_json
end

...

private

    def clan
      @clan ||= Clan.find(params[:clan_id])
    end

    def samurai
      @samurai ||= Samurai.find_by!(id: params[:id], clan_id: params[:clan_id])
    end

    def samurai_params
      params.permit(:name, :death_date, :armor_quality, :number_of_battles, :join_date)
    end

每当我运行rspec时,我都会收到此错误:

1) Clans::SamuraisController PUT #update with valid attributes updates samurai
     Failure/Error: expect(new_samurai.name).to eq(new_attributes[:name])

       expected: "Danny Boy"
            got: "Danny"

       (compared using ==)
     # ./spec/controllers/samurais_controller_spec.rb:84:in `block (4 levels) in <main>'

这让我有些困惑,因为当我在控制台中运行samurai.update! some_params时,它可以正常工作。

我认为问题出在测试套件中,它不以某种方式未更新正确的武士,所以我尝试使用以下方法进行存根:

before(:each) do
     Samurai.should_receieve(:find).and_return(new_samurai)
end

,但是它不起作用(已弃用)。

很高兴获得帮助。

更新:

# ../schema.rb
ActiveRecord::Schema.define(version: 2019_03_14_150921) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "clans", force: :cascade do |t|
    t.string "name", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "samurais", force: :cascade do |t|
    t.string "name", null: false
    t.integer "armor_quality", default: 0
    t.integer "number_of_battles", default: 0
    t.date "join_date"
    t.date "death_date"
    t.bigint "clan_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["clan_id"], name: "index_samurais_on_clan_id"
  end

  add_foreign_key "samurais", "clans"
end

1 个答案:

答案 0 :(得分:0)

您必须将控制器中的samurai_params方法更改为:

def samurai_params
  params.require(:samurai).permit(:name, :death_date, :armor_quality, :number_of_battles, :join_date)
end

或修改规范以直接发送新属性(不将其嵌套在哈希中):

request_params = { clan_id: new_samurai.clan_id, id: new_samurai.id }
request_params.merge!(new_attributes)
put :update, params: request_params