Friday, 15 January 2010

ruby on rails - Why are my Rspec tests failing? -



ruby on rails - Why are my Rspec tests failing? -

i writing little rails todo app practice. trying write own rspec tests various things instead of next tutorial. have few tests couple failing , can't figure out why. seek create posting question here lastly resort every time have asked question here have gotten solution problem.

here's code:

require 'spec_helper' describe "todospages" describe "home page" before { visit root_path } "should have content 'todo app'" page.should have_content('todo app') end "should have title tag" page.should have_selector 'title' end "should delete todo , redirect index" expect { click_link "delete lastly todo" }.to change(todo, :count).by(-1) response.should redirect_to :action => 'index' end describe "add todo" subject { page } before {click_button "add todo"} { should have_selector('div.alert.alert-success', text: 'todo created') } end "should have error message" pending end "should create todo" expect { click_button "add todo" }.to change(todo, :count).by(1) end end end todos_controller class todoscontroller < applicationcontroller def index @todo_items = todo.all @new_todo = todo.new render :index end def delete @todo_delete = todo.last @todo_delete.delete redirect_to :action => 'index' end def add together todo = todo.create(:todo_item => params[:todo][:todo_item]) unless todo.valid? flash[:error] = todo.errors.full_messages.join("<br>").html_safe else flash[:success] = "todo created" end redirect_to :action => 'index' end def finish params[:todos_checkbox].each |check| todo_id = check t = todo.find_by_id(todo_id) t.update_attribute(:completed, true) end redirect_to :action => 'index' end end index.html.erb <% @title = "todo app" %> <div class="container"> <div class="row"> <div class='span6' > <h1 class="hero-unit">simple todo app</h1> <p>all todos here</p> <%= form_for @new_todo, :url => { :action => "add" } |f| %> <%= f.text_field :todo_item %> <%= f.submit "add todo", class: "btn btn-primary" %> <%end%> <% if flash[:error] %> <div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert" >×</button> <strong><%= flash[:error] %></strong> </div> <% end %> <% if flash[:success] %> <div class="alert alert-success"> <button type="button" class="close" data-dismiss="alert" >×</button> <strong><%= flash[:success] %></strong> </div> <% end %> <div class="well"> <%= form_tag("/todos/complete/", :method => "post") %> <ul style="list-style-type:none;"> <% @todo_items.each |t| %> <% if t.completed == true %> <li style="color:grey;"> <%= check_box_tag "todos_checkbox[]",t.id %> <strike><%= t.todo_item %></strike> </li> <% else %> <li> <%= check_box_tag "todos_checkbox[]",t.id %> <%= t.todo_item %> </li> <% end %> <%end%> </ul> <%= submit_tag("complete todos", :class=>"btn btn-success") %> <%end %> </div> <!-- --> <%= link_to "delete lastly todo", delete_path %> </div> <!-- span6--> </div> <!-- row --> failures: 1) todospages home page should create todo failure/error: expect { click_button "add todo" }.to change(todo, :count).by(1) count should have been changed 1, changed 0 # ./spec/views/index_page_spec.rb:29:in `block (3 levels) in <top (required)>' 2) todospages home page should delete todo , redirect index failure/error: expect { click_link "delete lastly todo" }.to change(todo, :count).by(-1) nomethoderror: undefined method `delete' nil:nilclass # ./app/controllers/todos_controller.rb:11:in `delete' # (eval):2:in `click_link' # ./spec/views/index_page_spec.rb:14:in `block (4 levels) in <top (required)>' # ./spec/views/index_page_spec.rb:14:in `block (3 levels) in <top (required)>' 3) todospages home page add together todo failure/error: { should have_selector('div.alert.alert-success', text: 'todo created') } expected css "div.alert.alert-success" text "todo created" homecoming # ./spec/views/index_page_spec.rb:20:in `block (4 levels) in <top (required)>' finished in 0.33114 seconds 8 examples, 3 failures, 1 pending failed examples: rspec ./spec/views/index_page_spec.rb:28 # todospages home page should create todo rspec ./spec/views/index_page_spec.rb:13 # todospages home page should delete todo , redirect index rspec ./spec/views/index_page_spec.rb:20 # todospages home page add together todo randomized seed 32260

thanks in advance!

------todo.rb

class todo < activerecord::base attr_accessible :todo_item, :completed validates :todo_item, presence: true , length: { maximum: 25 } end

i'll take stab @ this. in no particular order:

test 2 (todospages home page should delete todo , redirect index)

it looks todo table empty in test database, when phone call todo.last in controller action, nil , seek , phone call delete on nil.

i can't see anywhere in test creating @ to the lowest degree 1 todo object before delete test.

on separate note, in controller may want consider using destroy instead of delete method.

test 1 (todospages home page should create todo)

what have in model todo in terms of validations? there required fields? i'm guessing :todo_item required field, in rspec test click on button , don't provide input field failing model validation.

in controller action add, utilize create! instead of create, , should give additional error messages if failing model validations.

test 3 (todospages home page add together todo )

may failing same reason above test failing, i.e. validations failing. prepare first , see if test passing.

hope helps.

edit: examples of tests passing params

my personal preference separate controller tests (testing specific controller functionality) integration type test (making sure pages populated properly, buttons appear , clickable etc). latter utilize cucumber. write much easier controller tests using rspec, allow test specific logic pass params straight action testing. test

describe "add todo" "increases count 1" expect {post: :add, todo_item: "string"}.to change(todo, :count).by(1) end end

alternatively if want go on doing integration test in rspec, (assuming using capybara simulating browser), need give todo_item text field unique id or name, , do:

it "should create todo" fill_in "*todo_item_unique_id" "some text" expect { click_button "add todo" }.to change(todo, :count).by(1) end

ruby-on-rails rspec

No comments:

Post a Comment