当我在我的规范中添加测试时,我开始怀疑每一个在我编写它时通过的测试,所以我在代码中添加了一些语法错误。事实证明,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 配置细节。知道为什么会这样吗?