0
votes

Try create rspec test for get method throught ajax. Retruns 302 status but expect 200. Test for Post method returns the same results. user_pages_spec.rb

require 'spec_helper'

describe "ProgectPage" do 

  subject { page }
  let(:user) { FactoryGirl.create(:user) }


  before(:each) do
    visit signin_path
      fill_in "Email",    with: user.email.upcase
      fill_in "Password", with: user.password
      click_button "Sign in"
  end

  describe "createProgect" do 
    describe "vith invalid data" do
      before do 
        click_link "Add TODO list" 
        fill_in "progect_title",    with: 'Progect new'
        fill_in "progect_date_end", with: ''
        click_button "Save"
      end
        it { should have_selector('div.alert.alert-error') }
    end
    describe "vith valid data" do
      before do 
        click_link "Add TODO list" 
        fill_in "progect_title",    with: 'Progect new'
        fill_in "progect_date_end", with: '2014-10-10'
      end
      it "should increment the progect count" do
          expect do
          click_button "Save"
        end.to change(user.progects, :count).by(1)
      end
    end
  end


  it "has a 200 status code" do
    #p user.inspect
    xhr :get, '/users/1' 
  #   p response.inspect
    response.code.should == "200"
  end

  # it "has a 200 status code" do
  #   xhr :post, '/progects', progect: {title: "First Progect", date_end: "2014-10-11", user_id: 1}
  #   p response.inspect
  #   response.code.should == "200"
  # end

end

rake routes

Prefix Verb   URI Pattern               Controller#Action
   duties_new GET    /duties/new(.:format)     duties#new
    users_new GET    /users/new(.:format)      users#new
        users GET    /users(.:format)          users#index
              POST   /users(.:format)          users#create
     new_user GET    /users/new(.:format)      users#new
    edit_user GET    /users/:id/edit(.:format) users#edit
         user GET    /users/:id(.:format)      users#show
              PATCH  /users/:id(.:format)      users#update
              PUT    /users/:id(.:format)      users#update
              DELETE /users/:id(.:format)      users#destroy
     sessions POST   /sessions(.:format)       sessions#create
  new_session GET    /sessions/new(.:format)   sessions#new
      session DELETE /sessions/:id(.:format)   sessions#destroy
     progects POST   /progects(.:format)       progects#create
      progect DELETE /progects/:id(.:format)   progects#destroy
       duties POST   /duties(.:format)         duties#create
         duty DELETE /duties/:id(.:format)     duties#destroy
         root GET    /                         sessions#new
       signin GET    /signin(.:format)         sessions#new
      signout DELETE /signout(.:format)        sessions#destroy
              PUT    /duties/:id(.:format)     duties#update
duties_updpos POST   /duties/updpos(.:format)  duties#updpos
              PUT    /progects/:id(.:format)   progects#update

bundle exec rspec spec/requests/user_pages_spec.rb

F..

Failures:

  1) ProgectPage has a 200 status code
     Failure/Error: response.code.should == "200"
       expected: "200"
            got: "302" (using ==)
     # ./spec/requests/user_pages_spec.rb:45:in `block (2 levels) in <top (required)>'

Finished in 0.6253 seconds
3 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:41 # ProgectPage has a 200 status code

UPD

it "has a 200 status code" do
  xhr :get, :show, id: 1
  response.code.should == "200"
end

have error

F..

Failures:

  1) ProgectPage has a 200 status code
     Failure/Error: xhr :get, :show, id: 1
     ArgumentError:
       bad argument (expected URI object or URI string)
     # ./spec/requests/user_pages_spec.rb:44:in `block (2 levels) in <top (required)>'

Finished in 0.6102 seconds
3 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:41 # ProgectPage has a 200 status code

Randomized with seed 13402
1

1 Answers

0
votes

Check you log file to be sure but I think you'll find that the 302 response you are getting is your login system redirecting you to the signup page.

The problem is that you are mixing 2 things in your specs: capybara (which you are using for logging in) and the rails integration testing helpers (when you call xhr). While they appear similar they are quite separate - one cannot see the cookies set by the other so your ajax request is denied for not being logged in. In general mixing the two in a single spec doesn't end well (I wouldn't even use both in the same file, maybe even same project because of the potential for confusion).

If you choose to fix this by going down the capybara way then, rather than making the ajax request directly, you would trigger it be clicking on a button (or whatever ui action should trigger it) and checking that the DOM changes in some way.

If you go down the integration testing route then you need to be making your login requests with the integration testing methods (get, post etc) or even by fiddling with the session directly. You might even decide that a controller spec is more suited to what you want to test