Javascript Rails为json api请求返回不正确的MIME类型(仅在测试中)
在我的应用程序中,我动态呈现外部小部件请求的javascript文件。在development env中一切正常,但当我测试时,出现错误:Javascript Rails为json api请求返回不正确的MIME类型(仅在测试中),javascript,ruby-on-rails,ruby-on-rails-5,mime,Javascript,Ruby On Rails,Ruby On Rails 5,Mime,在我的应用程序中,我动态呈现外部小部件请求的javascript文件。在development env中一切正常,但当我测试时,出现错误: Refused to execute script from 'http://localhost:3000/widget_init.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. 为什么它只出现在测试环境
Refused to execute script from 'http://localhost:3000/widget_init.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
为什么它只出现在测试环境中,以及如何手动设置MIME类型“text/javascript”
路线:
get 'widget_init', to: 'widget/root#init'
控制器:
def init
respond_to do |format|
format.html
format.js { render 'init' }
end
end
UPD:我的测试是否可以帮助您:
scenario 'opens widget if webpage id and token are valid', js: true do
webpage.url = 'http://localhost:3000/test_widget/with_script'
webpage.save
webpage.reload
visit workspace_webpage_path(webpage)
find('#open_webpage_button').click
#here should open a widget
expect(page).to have_selector('#follower_widget__script')
expect(page).to have_selector('#follower_widget__root')
end
我发现问题出在Chrome上,它打开了一个带有不正确信息的新标签页(例如,用户未登录,尽管我之前在同一测试中登录过) 手动添加mime类型没有帮助:format.js{render'init',mime_type:mime::type.lookup(“text/javascript”)}可以显示您的测试吗?Hi@kunashir!我更新了描述。主要的谜团是,只有在我运行rspec测试时才会出现此错误消息。