2016-09-19 2 views
0

.js.erb 형식으로 뷰를 렌더링하는 동작이있는 컨트롤러를 테스트하고 있습니다. 이러한 작업에 테스트는 다음과 같은 오류가 발생 :Ruby-on-rails 테스트 발생 JS 뷰를 렌더링 할 때 InvalidCrossOriginRequest

Minitest::UnexpectedError: ActionController::InvalidCrossOriginRequest: Security warning: an embedded tag on another site requested protected JavaScript. If you know what you're doing, go ahead and disable forgery protection on this action to permit cross-origin JavaScript embedding.

위조 보호 나는 그것의 확인 이러한 작업에 예외를 둘 때,이 오류가 발생합니다,하지만 난 그것을 사용하지 않으니까.

다음
class TasksController < ApplicationController 

    def new 
    # TODO add blah later 
    @task = Task.new 
    render 'tasks/show_form.js.erb' 
    end 

    def create 
    # FIXME there is bug here 
    @task = Task.new(task_params) 
    @task.user = current_user 
    authorize! :create, @task 
    save_task 
    end 

    def edit 
    @task = Task.find(params[:id]) 
    authorize! :edit, @task 
    render 'tasks/show_form.js.erb' 
    end 

    def update 
    @task = Task.find(params[:id]) 
    @task.assign_attributes(task_params) 
    authorize! :update, @task 
    save_task 
    end 

    def destroy 
    @task = Task.find(params[:id]) 
    authorize! :destroy, @task 
    @task.destroy 
    @tasks = Task.accessible_by(current_ability) 
    end 

    private 

    def save_task 
    if @task.save 
     @tasks = Task.accessible_by(current_ability) 
     render 'tasks/hide_form.js.erb' 
    else 
     render 'tasks/show_form.js.erb' 
    end 
    end 

    def task_params 
    params.require(:task).permit(:title, :note, :completed) 
    end 
end 

이 컨트롤러에 대한 내 테스트한다 :

require 'test_helper' 

class TasksControllerTest < ActionDispatch::IntegrationTest 
    #include Devise::Test::ControllerHelpers 

    def setup 
    @task = tasks(:one) 
    @password = "password" 
    @confirmed_user = User.create(email: "#{rand(50000)}@example.com", 
            password: @password, 
            confirmed_at: "2020-02-01 11:35:56") 
    @unconfirmed_user = User.create(email: "#{rand(50000)}@example.com", 
            password: @password, 
            confirmed_at: "") 
    sign_in(user: @confirmed_user, password: @password) 

    end 

    test "should get new" do 
    get new_task_url 
    assert_response :success 
    end 

    test "should create task" do 
    assert_difference('Task.count') do 

     post tasks_url, params: {task: {title: 'Dont fail test !'}} 
    end 

    assert_redirected_to task_url(Task.last) 
    end 

    test "should get edit" do 
    get edit_task_url(@task) 
    assert_response :success 
    end 

    test "should update task" do 
    patch task_url(@task), params: {task: {title: 'updated'}} 
    assert_redirected_to task_url(@task) 
    article.reload 
    assert_equal "updated", article.title 
    end 

    test "should destroy task" do 
    assert_difference('Task.count', -1) do 
     delete task_url(@task) 
    end 

    assert_redirected_to tasks_url 
    end 
end 

당신의이 오류를 해결하는 방법에 대한 아이디어가 있습니까 여기

내 컨트롤러?

는이 솔루션이 누군가에게 도움이 될 수 있습니다 내 컨트롤러

skip_before_action :verify_authenticity_token, :only => [:edit, :new]

희망의 시작 부분에 넣어했다

+0

의도적으로 앱에서 코러스를하고 싶습니까? 예기치 않게 발생합니까? cors를하고 싶다면 [rack-cors] (https://github.com/cyu/rack-cors)를 사용하는 것이 어떻습니까? – YTorii

+0

나는 내 자원 만 사용하기 때문에 예기치 않게 말할 것입니다. 도와 주셔서 감사합니다, 나는 해결책을 찾았습니다 :) –

답변

-1

사전에 감사

1
같은를 사용하도록 GET 요청을 변경하려고 할 수

브라우저가 AJAX 요청 인 XMLHttpRequest에 대한 메커니즘. 당신은 귀하의 경우에는 xhr

와 레일 테스트에서이 작업을 수행 할 수 있습니다 :이 의미

xhr :get, new_task_url 

당신이 레일 기본값은 당신의 테스트를 통과 얻을 우회되지 않습니다.