我必须测试我的Grape API请求,该请求还要调用服务,该服务保存broker_profile数据。当我使用夹具文件上传来上传图像时,它抛出<ArgumentError: wrong number of arguments (given 1, expected 0; required keywords: io, filename)>
错误,我最近从回形针迁移到了活动存储,然后才进行迁移测试。当前,Rails版本是2.6.6中的5.2.4.2 ruby版本]
基本上,问题是如何正确测试(Active Directory)使用Active Storage附加映像的API?
服务文件
class BrokerProfileService
def initialize(broker_profile)
@broker_profile = broker_profile
end
def self.create!(attrs)
new_broker = BrokerProfile.new
new(new_broker).update!(attrs)
new_broker
end
def update!(attrs)
@broker_profile.assign_attributes(attrs)
@broker_profile.save!
end
end
Rspec文件
describe API::V1::BrokerProfiles do
describe "POST /v1/broker_profiles" do
let(:headers) { auth_header_for(profile) }
let(:expect_brokerage_endpoint) { false }
let(:path) { "/v1/broker_profiles" }
let(:password) { nil }
let(:params) { {
broker_profile: {
first_name: "John",
last_name: "Doe",
email: "[email protected]",
password: password,
job_title: "Consultant",
phone: "+986455454",
location: "Abu Dhabi",
linkedin_url: "http://linkedin.com/111",
code: "333",
bio: "worked hard",
image: fixture_file_upload("#{fixtures_dir}/images/test.jpg", "image/jpeg")
}
}}
subject { post path, params: params, headers: headers }
context "as admin" do
let(:brokerage) { create(:brokerage) }
let(:profile) { create(:admin_profile, brokerage: brokerage) }
context "when params are correct" do
def expect_created(password_set: false)
subject
expect(response).to be_successful
broker_profile_payload = response_json["broker_profile"]
expect(broker_profile_payload).to be_a(Hash)
if expect_brokerage_endpoint
expect(broker_profile_payload["id"]).to eq nil
expect(broker_profile_payload["brokerage_name"]).to eq nil
else
expect(broker_profile_payload["id"]).to be_an(Integer)
expect(broker_profile_payload["brokerage_name"]).to eq profile.brokerage.name
end
expect(broker_profile_payload["code"]).to eq "333"
expect(broker_profile_payload["first_name"]).to eq "John"
expect(broker_profile_payload["last_name"]).to eq "Doe"
expect(broker_profile_payload["email"]).to eq "[email protected]"
expect(broker_profile_payload["is_password_set"]).to eq password_set
expect(broker_profile_payload["job_title"]).to eq "Consultant"
expect(broker_profile_payload["phone"]).to eq "+971503798758"
expect(broker_profile_payload["location"]).to eq "Abu Dhabi"
expect(broker_profile_payload["linkedin_url"]).to eq "http://linkedin.com/111"
expect(broker_profile_payload["bio"]).to eq "worked hard"
broker_profile = brokerage.broker_profiles.find_by_code(broker_profile_payload["code"])
expect(broker_profile.manually_created).to eq true
broker_profile
end
context "and password is not set" do
specify do
broker_profile = expect_created
expect(broker_profile.user.valid_password?(nil)).to eq false
end
end
end
end
end
有趣。我在以下测试中取得了成功。
RSpec.describe BrokersController, type: :controller do
render_views
before do
post :create, params: { broker: { name: "Test", image: fixture_file_upload("test-image.jpg") } }, format: :json
end
it "updates the broker successfully" do
parsed_response = JSON.parse(response.body)
expect(response).to be_successful
expect(parsed_response["image"]).to include("/rails/active_storage/blobs/")
expect(parsed_response["image"]).to include("test-image.jpg")
expect(parsed_response.keys).to eq(["name", "image"])
end
end
并且作为参考,test-image.jpg
存储在spec/fixtures/test-image.jpg
中。