Fixing RSPEC URI::InvalidURIError: bad URI(is not URI?): error for post request test
Fixing RSPEC URI::InvalidURIError: bad URI(is not URI?): error for post request test
我有一个 Rails API 可以抓取网站并将网站的文本内容存储到数据库中。我正在为创建路由编写 rspec 测试,但我不断收到错误消息:
Failure/Error: before { post 'POST /url_contents?url=www.google.com' }
URI::InvalidURIError:
bad URI(is not URI?): http://www.example.com:80POST /url_contents?url=www.google.com
但是,如果我使用提供的 URL 通过 Postman 自己发出 post 请求,它会成功。为什么 rspec 给我这个 URI 错误,我该如何解决?
This is how the test is written:
describe 'POST /url_contents' do
context 'when the url is valid' do
before { post 'POST /url_contents', params: "www.google.com" }
it 'returns a status code of 201' do
expect(response).to have_http_status(201)
end
end
end
控制器动作如下:
def create
scrapedContent = UrlContent.parser(url_params)
if scrapedContent == 403
render json: { messsage: "Invalid URL" }
else
newContent = UrlContent.new
binding.pry
newContent.content = scrapedContent.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
if newContent.save?
render json: {message: "Successfully added the url content"}, status: 201
else
render json: { message: "error, #{newContent.errors.full_messages}"}, status: 412
end
end
end
感谢您的见解!
http://www.example.com:80POST /url_contents?url=www.google.com
这是无效的 url.
您在 RSpec 中提出请求的方式有误:
context 'when the url is valid' do
# Change this line below
before { post :create, url: "www.google.com" }
it 'returns a status code of 201' do
expect(response).to have_http_status(201)
end
end
在编辑你的测试用例之前试试这个
before { post 'POST /url_contents', params: "www.google.com" }
这是编辑测试用例后的结果
before { post '/url_contents', params: "www.google.com" }
我有一个 Rails API 可以抓取网站并将网站的文本内容存储到数据库中。我正在为创建路由编写 rspec 测试,但我不断收到错误消息:
Failure/Error: before { post 'POST /url_contents?url=www.google.com' }
URI::InvalidURIError:
bad URI(is not URI?): http://www.example.com:80POST /url_contents?url=www.google.com
但是,如果我使用提供的 URL 通过 Postman 自己发出 post 请求,它会成功。为什么 rspec 给我这个 URI 错误,我该如何解决?
This is how the test is written:
describe 'POST /url_contents' do
context 'when the url is valid' do
before { post 'POST /url_contents', params: "www.google.com" }
it 'returns a status code of 201' do
expect(response).to have_http_status(201)
end
end
end
控制器动作如下:
def create
scrapedContent = UrlContent.parser(url_params)
if scrapedContent == 403
render json: { messsage: "Invalid URL" }
else
newContent = UrlContent.new
binding.pry
newContent.content = scrapedContent.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
if newContent.save?
render json: {message: "Successfully added the url content"}, status: 201
else
render json: { message: "error, #{newContent.errors.full_messages}"}, status: 412
end
end
end
感谢您的见解!
http://www.example.com:80POST /url_contents?url=www.google.com
这是无效的 url.
您在 RSpec 中提出请求的方式有误:
context 'when the url is valid' do
# Change this line below
before { post :create, url: "www.google.com" }
it 'returns a status code of 201' do
expect(response).to have_http_status(201)
end
end
在编辑你的测试用例之前试试这个
before { post 'POST /url_contents', params: "www.google.com" }
这是编辑测试用例后的结果
before { post '/url_contents', params: "www.google.com" }