RSpec生成的测试“将所请求的experience_level分配为@experience_level”是什么意思?

时间:2013-10-10 20:53:09

标签: ruby-on-rails rspec

我正在尝试通过此规范,但我不知道这意味着什么。这是完整的规格。 第二个示例是失败的示例。

describe "PUT update" do
    describe "with valid params" do
      it "updates the requested experience_level" do
        experience_level = ExperienceLevel.create! valid_attributes
        # Assuming there are no other experience_levels in the database, this
        # specifies that the ExperienceLevel created on the previous line
        # receives the :update_attributes message with whatever params are
        # submitted in the request.
        ExperienceLevel.any_instance.should_receive(:update_attributes).with({ "name" => "MyString" })
        put :update, {:id => experience_level.to_param, :experience_level => { "name" => "MyString" }}
      end

      it "assigns the requested experience_level as @experience_level" do
        experience_level = ExperienceLevel.create!(name: 'test'), valid_attributes
        put :update, {:id => experience_level.to_param, :experience_level => valid_attributes}
        assigns(:experience_level).should eq(experience_level)
      end

      it "redirects to the experience_level" do
        experience_level = ExperienceLevel.create! valid_attributes
        put :update, {:id => experience_level.to_param, :experience_level => valid_attributes}
        response.should redirect_to(experience_level)
      end
    end

以下是终端中的消息:

1) ExperienceLevelsController PUT update with valid params assigns the requested experience_level as @experience_level
     Failure/Error: assigns(:experience_level).should eq(experience_level)

       expected: [#<ExperienceLevel id: 1, name: "test", description: nil, created_at: "2013-10-10 20:40:05", updated_at: "2013-10-10 20:40:05">, {"name"=>"MyString"}]
            got: #<ExperienceLevel id: 1, name: "MyString", description: nil, created_at: "2013-10-10 20:40:05", updated_at: "2013-10-10 20:40:05">

       (compared using ==)

       Diff:
       @@ -1,3 +1,2 @@
       -[#<ExperienceLevel id: 1, name: "test", description: nil, created_at: "2013-10-10 20:40:05", updated_at: "2013-10-10 20:40:05">,
       - {"name"=>"MyString"}]
       +#<ExperienceLevel id: 1, name: "MyString", description: nil, created_at: "2013-10-10 20:40:05", updated_at: "2013-10-10 20:40:05">

     # ./spec/controllers/experience_levels_controller_spec.rb:100:in `block (4 levels) in <top (required)>'

1 个答案:

答案 0 :(得分:1)

第二个例子中的以下陈述:

experience_level = ExperienceLevel.create!(name: 'test'), valid_attributes

与:

相同
experience_level = [ExperienceLevel.create!(name: 'test'), valid_attributes]

换句话说,它是从赋值运算符右侧的两个逗号分隔值创建一个数组,并将该数组赋给experience_level。这至少是您的测试失败的一个原因。