hello all, i am new to RoR programming and am struck with the functional test whole checking for uniqeness of carriers. If it is not unique,the the bin shudn''t be created.Can someone help me in correcting my code. def test_create_failure_cycle_unique begin num_bins = Bin.count post :create, {:bin => { :carrier => carriers(:A), :cycle => cycles(:one), :name => "701", :start => "1:00", :end => "1:01" }}, { :user => @user_id, :division => @division_id } validates_uniqueness_of :cycles assert_response :redirect assert_redirected_to :action => ''list'' assert_equal num_bins + 1, Bin.count rescue exception: NoMethodError assert_response :error flash[:notice]="Cycle name not unique." end end 1) Error: test_create_failure_cycle_unique(BinsControllerTest): NameError: undefined local variable or method `exception'' for #<BinsControllerTest:0x48077e0> c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:471:in `method_missing'' bins_controller_test.rb:119:in `test_create_failure_cycle_unique'' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `__send__'' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run'' 14 tests, 28 assertions, 0 failures, 1 errors>Exit code: 1Thanx a lot. Regards, cutelucks -- Posted via http://www.ruby-forum.com/.
On Apr 21, 6:17 am, cutelucks Ms <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> hello all, > > i am new to RoR programming and am struck with the functional test whole > checking for uniqeness of carriers. If it is not unique,the the bin > shudn''t be created.Can someone help me in correcting my code. > > def test_create_failure_cycle_unique > > begin > num_bins = Bin.count > > post :create, {:bin => { :carrier => carriers(:A), :cycle => > cycles(:one), :name => "701", :start => "1:00", :end => "1:01" }}, { > :user => @user_id, :division => @division_id } > validates_uniqueness_of :cycles > assert_response :redirect > assert_redirected_to :action => ''list'' > > assert_equal num_bins + 1, Bin.count > > rescue exception: NoMethodError > assert_response :error >I''m a bit confused as to what you''re actually doing ? why are you calling validates_uniqueness_of from the test ( validations are added inside models) ? Why do you have 2 assert_responses of different type ? Why are you rescuing NoMethodError (as you''re finding out, that ''s not the syntax for rescuing exceptions: def some_method ... rescue SomeException ... end or def some_method begin ... rescue SomeException ... end end do that, and if you need the exception raise you can do def some_method begin ... rescue SomeException => e ... end end ) Fred> flash[:notice]="Cycle name not unique." > end > end > > 1) Error: > test_create_failure_cycle_unique(BinsControllerTest): > NameError: undefined local variable or method `exception'' for > #<BinsControllerTest:0x48077e0> > c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_ process.rb:471:in > `method_missing'' > bins_controller_test.rb:119:in `test_create_failure_cycle_unique'' > c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testi ng/setup_and_teardown.rb:60:in > `__send__'' > c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testi ng/setup_and_teardown.rb:60:in > `run'' > > 14 tests, 28 assertions, 0 failures, 1 errors > > >Exit code: 1 > > Thanx a lot. > > Regards, > cutelucks > -- > Posted viahttp://www.ruby-forum.com/.
The short answer is, replace> rescue exception: NoMethodErrorwith rescue NoMethodError (or rescue NoMethodError => exception if you meant ''exception'' as the variable assigned to it). The long answer, as Fred put it, is why? Validations should be performed by the model and don''t really make sense in a test. You are asserting two different responses to the same post request, so one of them must always fail. You are rescuing NoMethodError, which is thrown when a nonexistant method is called. Why exactly would you not want that to raise an error? If NoMethodError is raised by your validation, I''d recommend changing that. If the validation is in the model (as it should be), then after assigning parameters as necessary to bin = Bin.new(params as create), use if bin.save # Saved else # Validation error end and have your tests check handling based on that. -- Posted via http://www.ruby-forum.com/.