尝试通过 ajax 为 get 方法创建 rspec 测试

Try create rspec test for get method throught ajax

尝试通过 ajax 为 get 方法创建 rspec 测试。返回 302 状态但期望 200。测试 Post 方法 returns 相同的结果。 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

耙路

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

捆绑执行 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

有错误

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

检查您的日志文件以确保确定,但我认为您会发现您收到的 302 响应是您的登录系统将您重定向到注册页面。

问题是您在规范中混合了 2 个东西:水豚(您用于登录)和 rails 集成测试助手(当您调用 xhr 时)。虽然它们看起来相似,但它们是 quite 分开的 - 一个无法看到另一个设置的 cookie,因此您的 ajax 请求因未登录而被拒绝。通常将两者混合在一个规范中不会不会很好地结束(我什至不会在同一个文件中使用两者,甚至可能是同一个项目,因为可能会造成混淆)。

如果您选择沿着水豚的方式解决这个问题,那么,与其直接发出 ajax 请求,您可以通过点击按钮(或任何 ui 应该执行的操作来触发它触发它)并检查 DOM 是否以某种方式发生变化。

如果您走集成测试路线,那么您需要使用集成测试方法(get、post 等)发出登录请求,甚至直接摆弄会话。您甚至可以决定控制器规格更符合您要测试的内容ui