0

我正在尝试更新请求规范中的记录,但它没有更新。在现实生活中这样做是有效的。这是规格:

describe "sessions" do
  before do
    @user = FactoryGirl.create(:user)
    @api_key = FactoryGirl.create(:api_key)
  end

  it "is updated properly" do
    put "/api/v1/users/#{@user.id}?user_email=#{@user.email}&auth_token=#{@user.authentication_token}", {user: {name: "New Name"}},{ "HTTP_AUTHORIZATION"=>"Token token=\"#{@api_key.access_token}\"" }
    @user.name.should eq("New Name")
    response.status.should be(201)
  end
end

上述测试失败并出现以下错误:

Failure/Error: @user.name.should eq("New Name")

       expected: "New Name"
            got: "nil"

       (compared using ==)

名称是一个可选参数,所以我只是不在工厂中设置它。例如,如果我确实设置了它,则该行说got: "Bill"

并且,为了完整起见,这里是工厂:

FactoryGirl.define do
  sequence :email do |n|
    "test#{n}@vitogo.com"
  end

      factory :user do
        email           
        password                            '12345678'
        password_confirmation '12345678'
        goal_id                             1
        experience_level_id     1
        gender                              'Female'
        factory :admin do
            after(:create) { |user| user.role = 'admin'; user.save }
        end
      end
    end

    FactoryGirl.define do
      factory :api_key do
        access_token "MyString"
      end
    end
4

1 回答 1

1

您只需要@user在 PUT 调用后重新加载,例如@user.reload.

于 2013-10-27T20:36:08.853 回答