简体   繁体   中英

Ruby on Rails - Rspec 3.8, Rails 5, FacotryBot POST redirect test failure

I am following the Ruby on Rails restaurantly tutorial found here: http://rubyonrailstutor.github.io/ . First of all I have had to make some changes, much by trial and error and searching errors online. Much of it is due to the fact that it's an old tutorial on an older version of Ruby and Rails.

I am using rails 5.2.1 and Ruby 2.5.3 as well as the latest version of most of the gems. I have successfully completed the tutorial up to the last rspec test found here: http://rubyonrailstutor.github.io/new/restaurants-new-create/ .

When I run rspec spec/requests/restaurants_spec.rb I get the following error:

Failures:

  1) RestaurantsController POST /restaurants complete params redirects to show
     Failure/Error: expect(subject).to redirect_to restaurant_path(id:1)

       Expected response to be a redirect to <http://www.example.com/restaurants/1> but was a redirect to <http://www.example.com/restaurants/new>.
       Expected "http://www.example.com/restaurants/1" to be === "http://www.example.com/restaurants/new".
     # ./spec/requests/restaurant_spec.rb:32:in `block (4 levels) in <top (required)>'

Finished in 0.60375 seconds (files took 1.47 seconds to load)
5 examples, 1 failure

Failed examples:

rspec ./spec/requests/restaurant_spec.rb:31 # RestaurantsController POST /restaurants complete params redirects to show

My controller is the same as what the author of the tutorial is using: https://github.com/rubyonrailstutor/restaurantly/blob/restaurants-new/app/controllers/restaurants_controller.rb .

The part of my spec file which is failing is:

context "POST /restaurants" do
    context "complete params" do
      subject{ post "/restaurants", params: { name: "mcrails"}}
      it "redirects to show" do
        expect(subject).to redirect_to restaurant_path(id:1)
      end 
    end 

    context "incomplete params" do
      subject{ post "/restaurants", {}} 
      it "redirects to new" do
        expect(subject).to redirect_to(new_restaurant_path)
      end 
    end 
  end 

My subject is slightly different than how the author uses it and I am wondering of this is what I am getting wrong. I changed it when I was getting a number of arguments error which I believe is due to versioning differences. The original looks like this:

restaurant = {restaurant: {name: "mcrails"}}
subject{ post "/restaurants", restaurant }

Which I have changed to this:

subject{ post "/restaurants", params: { name: "mcrails"}}

The author's spec file on rails 4: https://github.com/rubyonrailstutor/restaurantly/blob/restaurants-new/spec/requests/restaurant_spec.rb .

When I am running the web server and create a new item it redirects to #show the item properly. However the rspec test fails. So I believe I am not running the test properly. Perhaps due to the version difference and the params I am passing but I can't figure this one out. My Rails experience is very minimal at this point.

@MarkMerritt pointed me to documentation that helped me with some trial and error figure out how to make it work.

While I still don't completely understand the differences in syntax between the tutorial and what I was able to get working I will post my final spec file edits in case anyone has any feedback.

context "POST /restaurants" do
    context "complete params" do
      subject { post "/restaurants", :params => { :restaurant => { :name => "mcrails" } } } 

      it "redirects to show" do
        expect(subject).to redirect_to restaurant_path(:id => assigns(:restaurant).id) 
      end 
    end 

    context "incomplete params" do
      subject { post "/restaurants", :params => {} } 

      it "redirects to new" do
        expect(subject).to redirect_to(new_restaurant_path) 
      end 
    end 
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM