使用Guard和Spork的Rspec不会运行某些测试,也不会为失败提供详细的错误声明

时间:2013-06-04 22:11:11

标签: ruby-on-rails rspec guard spork

当我在我的规范中添加测试时,我开始怀疑我写的每一个传递,所以我在代码中添加了一些语法错误。事实证明,Rspec没有运行我最近的测试,因此语法错误没有得到解决。下面代码中的注释显示Rspec停止显示绿色周期的任意行或测试的红色F:

require 'spec_helper'

describe Api::PagesController do
  def valid_session
    {}
  end

  describe "GET index" do
    before :each do
      @page = create(:page)
    end
    it "assigns all pages as @pages" do
      get :index
      assigns(:pages).should eq([@page])
    end
    it "returns json" do
      get :index, format: :json
      expect(response.body).to have_content @page.to_json
    end
  end

  describe "GET show" do
    before :each do
      @page = create(:page)
    end
    it "assigns the requested page as @page" do
      get :show, {:id => @page.to_param}, valid_session
      assigns(:page).should eq(@page)
    end

    it "returns json" do
      get :show, {:id => @page.to_param}, valid_session, format: :json
      expect(response.body).to have_content @page.to_json
    end
  end

  describe "GET new" do
    it "assigns a new page as @page" do
      get :new, {}, valid_session
      assigns(:page).should be_a_new(Page)
    end
  end

  describe "GET edit" do
    before :each do
      @page = create(:page)
    end
    it "assigns the requested page as @page" do
      get :edit, {:id => @page.to_param}, valid_session
      assigns(:page).should eq(@page)
    end

    it "returns json" do
      get :edit, {:id => @page.to_param}, valid_session, format: :json
      expect(response.body).to have_content @page.to_json
    end
  end

  describe "POST create" do
    describe "with valid params" do
      it "creates a new Page in the database" do
        expect {
          post :create, {page: attributes_for(:page)}, valid_session
          }.to change(Page, :count).by(1)
      end

      it "assigns a newly created page as @page" do
        post :create, {page: attributes_for(:page)}, valid_session
        assigns(:page).should be_a(Page)
        assigns(:page).should be_persisted
      end

      #################### TESTS BELOW HERE ARE NOT SHOWN BY RSPEC ##########

      it "returns json" do
        expect{
          post :create, {page: attributes_for(:page)}, valid_session
          }.to have_content page.to_json
      end
    end
    describe "with invalid params" do 
      it "does not save the new page in the database" do
        expect {
          post :create, {page: attributes_for(:page_invalid)}, valid_session
          }.to_not change(Page, :count).by(1)
      end

      #FUTURE TEST - redirects to new page with errors
    end
  end

  describe "PUT update" do
    before :each do
      @page = create(:page)
    end
    describe "with valid params" do
      it "updates the requested page" do 
        Page.any_instance.should_receive(:update_attributes).with({ "title" => "MyString" })
        put :update, {:id => @page.to_param, :page => { "title" => "MyString" }}, valid_session
      end

      it "assigns the requested page as @page" do
        put :update, {:id => @page.to_param, :page => { "title" => "MyString" }}, valid_session
        assigns(:page).should eq(@page)
      end

      it "returns json" do
        put :update, {:id => @page.to_param, :page => { "title" => "MyString" }}, valid_session, format: :jason
        expect(response.body).to have_content @page.to_json
      end

    end

    describe "with invalid params" do
      it "assigns the page as @page" do 
        # Trigger the behavior that occurs when invalid params are submitted
        Page.any_instance.stub(:save).and_return(false)
        put :update, {:id => @page.to_param, page: attributes_for(:page_invalid)}, valid_session
        assigns(:page).should eq(@page)
      end

      #FUTURE TEST - redirects to edit
    end
  end

  describe "DELETE destroy" do
    before :each do
      @page = create(:page)
    end
    it "destroys the requested page" do
      expect {
        delete :destroy, {:id => @page.to_param}, valid_session
      }.to change(Page, :count).by(-1)
    end
  end

  describe "GET published" do
    before :each do 
      @page = create(:page)
      @page_unpublished = create(:page_unpublished)
    end
    it "returns a list of published pages only" do
      get :published, format: :json
      assigns(:pages).should eq([@page])
      expect(response.body).to have_content @page_unpublished.to_json
    end
  end

  describe "GET unpublished" do
    before :each do
      @page = create(:page)
      @page_unpublished = create(:page_unpublished)
    end
    it "returns a list of unpublished pages only" do
      get :unpublished, format: :json
      assigns(:pages).should eq([@page_unpublished])
      expect(response.body).to have_content @page_unpublished.to_json
    end
  end

  describe "GET total_words" do
    before :each do
      @page = create(:page)
    end
    it "returns a list of unpublished pages only" do
      get :total_words, {:id => @page.to_param}, format: :json
      assigns(:total_words).should eq([@page_unpublished])
      expect(response.body).to have_content @page_unpublished.to_json
    end
  end
end

当我故意在早期测试中添加语法错误时,我看到红色Fs而不是绿色点,但没有报告错误。早些时候,当出现错误时,我会看到(例如):

16:10:14 - INFO - Running: spec/models/page_spec.rb
Running tests with args ["--drb", "-f", "progress", "-r", "/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/guard-rspec-1.2.1/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec/models/page_spec.rb"]...
.......F

Failures:

  1) Page Publishing returns unpublished pages
     Failure/Error: expect(Page.unpublished).to eq [page_unpublished]
     NameError:
       undefined local variable or method `page_unpublished' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x0000010355b8c0>
     # ./spec/models/page_spec.rb:58:in `block (3 levels) in <top (required)>'

Finished in 0.54212 seconds
8 examples, 1 failure

Failed examples:

rspec ./spec/models/page_spec.rb:57 # Page Publishing returns unpublished pages

Done.

现在,当测试失败时,我只看到:

17:55:26 - INFO - Running: spec/controllers/pages_controller_spec.rb
Running tests with args ["--drb", "-f", "progress", "-r", "/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/guard-rspec-1.2.1/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec/controllers/pages_controller_spec.rb"]...
.....FF..Done.

此更改发生在编码时非常随意,我没有修改Guardfile或任何Spork配置细节。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

看起来rspec的一个参数是--out / dev / null。因此,您的输出将从STDOUT重定向。

一种可能性是,这是由一个或多个不良测试引起的。您是否尝试过评论各种测试以确定是否可以隔离有问题的测试?如果你按顺序运行测试,我会从rspec过早退出之前和之后的那些开始。