Post to another controller in Request Specs

66 views
Skip to first unread message

ahmed....@imerir.com

unread,
Dec 29, 2016, 1:19:55 PM12/29/16
to rspec
Hello i have two controllers : 

CalendarsController and EventsController

I'm writing a request spec where i try to create an event but before that i should create a Calendar to get an id.

So i do in my requests specs : 

RSpec.describe "Event Management", :type => request do 

context "with access token" do
  it "creates a calendar"
    post calendars_create_path, params: {name: "test"}
    expect(response.status).to eq(201)
  end
end

and in the create method of calendars controller i do : 

render json: @data, status: response.status

When i test the same test from above in the Calendars Controller spec it works fine and i get the right status code (201)

But in the requests specs it sends back a 307 http redirect code (it triest to redirect to GET /calendars/create) for some reasons even tho i have this in routes.rb : 

          calendars_index GET      /calendars/index(.:format)           calendars#index
            calendars_new GET      /calendars/new(.:format)             calendars#new
         calendars_create POST     /calendars/create(.:format)          calendars#create
         calendars_update PATCH    /calendars/update(.:format)          calendars#update
        calendars_destroy DELETE   /calendars/destroy(.:format)         calendars#destroy
           calendars_show GET      /calendars/show(.:format)            calendars#show
           calendars_edit GET      /calendars/edit(.:format)            calendars#edit
         calendars_delete GET      /calendars/delete(.:format)          calendars#delete

so i don't understand why its trying to redirect. Any ideas ?

Thanks in advance.

Aaron Kromer

unread,
Dec 29, 2016, 2:03:37 PM12/29/16
to rs...@googlegroups.com
Does your controller support both HTML and JSON responses?

Based on your snippets you aren't setting the Accept header in the request. If a default format isn't set in the routes then Rails will assume HTML. If the controller then checks the format it will use the HTML response path.

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/de2b037e-3f70-4989-8cbf-47171e2af697%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ahmed....@imerir.com

unread,
Dec 30, 2016, 1:41:51 AM12/30/16
to rspec
Ok this was solved, weird issue. I used microsoft ruby sample office 365 project as a starting project, and the post couldn't work. So i made a new project and copied my code from ruby sample project to new created project and it works.

Aaron Kromer

unread,
Dec 30, 2016, 9:47:31 AM12/30/16
to rs...@googlegroups.com
Glad you worked it out. Without knowing which tutorial it's hard to say what the root cause was for sure. I did see this sample project which has a `CalendarController` (https://github.com/jasonjoh/o365-tutorial). Looking at that they expect a form of auth to be set in the request. If it's not present it redirects. That's another possible reason why the request spec was redirecting.

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages