As I added tests to my spec, I became suspicious about every single one passing just as I wrote it, so I added some syntax errors into the code. It turned out, Rspec was not running my recent tests so the syntax errors weren't getting picked up. The comment in the code below shows the arbitrary line at which Rspec stopped showing a green period or red F for a test:
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
When I intentionally added syntax errors to earlier tests, I saw red Fs instead of the green dots, but no error was being reported. Earlier, when there was an error, I'd see (for example):
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.
Now, when a test fails, I only see:
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.
This change happened at a pretty arbitrary time while coding, I didn't modify the Guardfile or any Spork configuration details. Any idea why this is happening?
It looks like one of the arguments to rspec is --out /dev/null.
So your output is being redirected from STDOUT.
One possibility is that this is being caused by one or more bad tests. Have you tried commenting out various tests to see if you can isolate the problematic ones? If you're running your tests in order, I'd start with the ones just before and after the point at which rspec exits prematurely.
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.