Andrew Rosa
March 07, 2015
430

Property Based Testing on Rails

The Ruby community is known by its strong culture of testing, but this is not an excuse to refuse alternative approaches. One of these are Property Testing. In those tools we do not express examples, but the possible input formats; instead of specific outputs, characteristics that must remain true. In this way our tools are capable of exhaustively generate test cases and look for failures. In this talk we'll see how to create lean suites with wide coverage using Clojure's power to test a Rails application.

March 07, 2015

Transcript

8. it "sorts a list" do v = [3, 1, 2]

s = sort(v) expect(s).to eq([1, 2, 3]) end
9. it "sorts a list" do v = [3, 1, 2]

s = sort(v) expect(s).to eq([1, 2, 3]) end
10. it "sorts a list" do v = [3, 1, 2]

s = sort(v) expect(s).to eq([1, 2, 3]) end
11. it "sorts a list" do v = [3, 1, 2]

s = sort(v) expect(s).to eq([1, 2, 3]) end
12. it "sorts a list" do v = [3, 1, 2]

s = sort(v) expect(s).to eq([1, 2, 3]) end

14. #sorts ascending sorts an empty list sorts a list with

single item sorts a list with multiple items ! Finished in 0.00179 seconds 3 examples, 0 failures

(b + a)

(b + a)

(b + a)

(b + a)

(b + a)

(b + a)

23. ∀ v → vector int each pair a, b in

sort(v) a <= b
24. ∀ v → vector int each pair a, b in

sort(v) a <= b
25. ∀ v → vector int each pair a, b in

sort(v) a <= b
26. ∀ v → vector int each pair a, b in

sort(v) a <= b
27. ∀ v → vector int each pair a, b in

sort(v) a <= b
28. ∀ v → vector int each pair a, b in

sort(v) a <= b

30. ∀ v → vector int each pair a, b in

sort(v) a <= b
31. ∀ v → vector int each pair a, b in

sort(v) a <= b v → [] v → [-1] v → [2 -1 1 -1 -2 5] v → [4] v → [7 9 -4 -4] …

33. [23 -17 8 -20 -35 -25 -22 10 11 -19

9 -3 -42 -33 -34 -11 7 -28 -5 38 39 -26 -29 42 0 -41 33 -35 19]

37. [23 -17 8 -20 -35 -25 -22 10 11 -19

9 -3 -42 -33 -34 -11 7 -28 -5 38 39 -26 -29 42 0 -41 33 -35 19]

41. 01 def sort(v) 02 if v.include?(42) 03 v 04 else

05 v.sort 06 end 07 end
42. 01 def sort(v) 02 if v.include?(42) 03 v 04 else

05 v.sort 06 end 07 end v1 v2 v3 v4 …

UI

54. (defmulti dispatch! :action) ! (defmethod dispatch! :add-entry [action] (to base-url)

(input-text “#entry_value" (str (:value action))) (submit "#entry_value"))
55. (defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions)

(= (total-of actions) (total-recorded-in test-db))))
56. (defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions)

(= (total-of actions) (total-recorded-in test-db))))
57. (defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions)

(= (total-of actions) (total-recorded-in test-db))))
58. (defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions)

(= (total-of actions) (total-recorded-in test-db))))
59. (defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions)

(= (total-of actions) (total-recorded-in test-db))))
60. (defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions)

(= (total-of actions) (total-recorded-in test-db))))
61. (defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions)

(= (total-of actions) (total-recorded-in test-db))))