Successfully reported this slideshow.
Your SlideShare is downloading. ×

Spec & Test Helper

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
10 Email Etc
10 Email Etc
Loading in …3
×

Check these out next

1 of 20 Ad

More Related Content

Advertisement

Similar to Spec & Test Helper (20)

Recently uploaded (20)

Advertisement

Spec & Test Helper

  1. 1. Spec & test helpers
  2. 2. Hello my name is <ul><li>Michael Grosser </li></ul><ul><li>TU-Dresden / TFH-Berlin </li></ul><ul><li>Rathershort.com </li></ul>
  3. 3. Motivation <ul><li>Write less, test more </li></ul><ul><li>Rememberable syntax </li></ul><ul><li>Easy setup </li></ul><ul><li>Helpful failure messages </li></ul><ul><li>(Fast test runs) </li></ul>
  4. 4. Spec & test helpers <ul><li>Model Validation, Valid, Stay valid </li></ul><ul><li>Controller Resource, Misc, it renders, Response enhancers </li></ul><ul><li>Views Simple tests, Mails </li></ul>
  5. 5. Model – Validation <ul><li>assert_invalid_attributes User, :login=>['',nil,'admin'], :email=>['',nil,'aa'] </li></ul><ul><li><User.login> expected to be invalid when set to <admin> </li></ul>
  6. 6. Model – Valid <ul><li>fixtures = slow </li></ul><ul><li>ad-hoc building = error_prone && not DRY </li></ul><ul><li>Store valid sets of attributes in valid.yml </li></ul>
  7. 7. Model – Valid <ul><li>address: name: Micha mueller street: meine strasse additional: zusatz city: stadt zip: 13088 country: DE addressable_id: 1 addressable_type: Organisation </li></ul>
  8. 8. Model – Valid <ul><li>post :create, :user=>valid_attributes(User) </li></ul><ul><li>user = valid User </li></ul><ul><li>user = valid User, :name=>'too short' </li></ul><ul><li>user = create_valid User, :status=>'offline' </li></ul>
  9. 9. Model – Stay Valid <ul><li>rake db:validate_fixtures </li></ul><ul><li>rake db:validate_models </li></ul><ul><li>-- records - model -- 4x Access 2x Address 2x Attachment ... </li></ul>
  10. 10. Controller - Resource <ul><li>make_resourceful OR resource_controller </li></ul><ul><li>Tests ? </li></ul><ul><li>”just test what you have added yourself” </li></ul><ul><li>no-test-ache ? </li></ul><ul><li>worry about 'small' changes ? </li></ul>
  11. 11. Controller - Resource <ul><li>describe UsersController do before(:each) do login_as(:quentin) @item = stub_model(User,:to_param=>'1') end end </li></ul><ul><li>behave_like_resource </li></ul><ul><li>behave_like_resource_show </li></ul>
  12. 12. Controller – Misc - it_... <ul><li>response.should render_template('show') </li></ul><ul><li>response.should redirect_to(movie_path(@movie)) </li></ul><ul><li>flash[:notice].should_not be_blank </li></ul>
  13. 13. Controller – Misc - it_... <ul><li>it_renders :show </li></ul><ul><li>it_redirects @user </li></ul><ul><li>it_flashes :notice </li></ul><ul><li>it_assigns :user, @user </li></ul>
  14. 14. Controller – Response enhancers <ul><li>response.should render_template('index') </li></ul><ul><li>Expected to render index, got nil </li></ul><ul><li>response.should be_success </li></ul><ul><li>Expected true, got false </li></ul>
  15. 15. Controller – Response enhancers <ul><li>Status should be redirect but was 200(success) - rendered addresses/new - Flash: :error = Address contains errors! - Errors:Errors on @address(Address): City can't be blank </li></ul>
  16. 16. Views - Simple <ul><li>before(:each) do assigns[:dvds] = dvds(:one) assigns[:current_user] = users :quentin @path = 'dvds/' end </li></ul><ul><li>it &quot;renders show&quot; do render @path+&quot;show.haml&quot; end </li></ul>
  17. 17. Views - Mails <ul><li>it &quot;contains link to movie&quot; do deliver(:release_notification,@user,@movie) @text.should=~%r[movies/#{@movie.id}] end </li></ul>
  18. 18. Views - Mails <ul><li>def deliver(action, *objs) raise &quot;no mailer&quot; unless @mailer @mailer.send(&quot;deliver_#{action.to_s}&quot;,*objs) @delivery = ActionMailer::Base.deliveries.last @text = @delivery.body_port.to_s end </li></ul><ul><li>def mails ActionMailer::Base.deliveries end </li></ul>
  19. 19. Downloads / Details <ul><li>pragmatig.wordpress.com </li></ul><ul><li>[email_address] </li></ul>
  20. 20. Ende <ul><li>Danke </li></ul>

×