Is it possible to run loops in functional testing methods?

Is it possible (conceptually) to run loops in test methods?

I would like to check the range of parameter values ​​in the controller to determine if different inputs believe the correct values.

  test "logged in user - add something - 0 qty" do
    @app = Factory.create(:app)

    (0..5).each do |t|
      @qty = t
      login(:user)
      get :add. :id => @app.id, :qty => @qty
      assert_nil(flash[:error])
      assert_response :redirect
      assert_redirect_to :controller => :apps, :action => :show, :id => @app.id
      if @qty = 0 
        assert_equal(Invite.all.count, @qty + 1)
      else 
        assert_something .........
    end
  end

Something like that.

+3
source share
8 answers

. , , , , , , . , , - , , . , , . , .

:

, , . - concurrency. , - .

+6

() ?

, ?

, ? , .

+2

" " , , .

+2

. , . Cucumber, , .

, , , . , .

, , . , , . , , , . , , , .

+2

, , ( ). , .

+1

, , . , . , . , , :

  • 3 , ?
  • , ( 3,5 )?

. -, ? ? , ? , ?
, , , ( )? ? ? ? , , - , , ? ?

, . , , . . , . . . . . . , .

. , , - < 0,100 > (0..5 , ). . :

  • = -1
  • = 0
  • = 1
  • = 99
  • = 100
  • = 101

, , , .
, , (10,70), . . , , .
, , , , , , .

+1

. . , DRY, : , , ?

, . , :

  def test_swap_name
    test_cases = [
      [
        'Paul, Ron P.A.',
        'Ron Paul PA'
      ],
      [
        "PUBLIC, SR., JOHN Q",
        "JOHN Q PUBLIC SR"
      ],
      [
        "SMITH, JR., MARK A",
        "MARK A SMITH JR"
      ],
      [
        'James Brown',
        'James Brown'
      ],
      # (more test cases)
    ]
    for original, swapped in test_cases
      assertInfo("For original = #{original.inspect}") do
        assertEquals(original.swap_name, swapped)
      end
    end
  end

assertInfo . , , :

./StringUtil.test.rb
Method "test_swap_name" failed:
Assert::BlownAssert: For original = "Paul, Ron P.A.": Expected:
"Ron Paul PA"
but got
"Paul, Ron P.A."
./../../testlib/Assert.rb:125:in `fail_test'
./../../testlib/Assert.rb:43:in `assertEquals'
./StringUtil.test.rb:627:in `test_swap_name'
+1

, . , :

test "something" do
  for item in @collection
    assert_something item
  end
end

:

test "something" do
  for item in @collection
    assert_something item
    assert_something_else item
  end
end

:

test "something" do
  for item in @collection
    assert_something item
    if x == y
      assert_something_else item
    end
  end
end

test "something" do
  for item in @collection
    assert_something item
  end
  for item in @collection
    assert_something_else item
  end
end

, , , , . , , . , , , , . , Foo, @collection.first. , , item . , :

test "items are all Foo" do
  for item in @collection
    assert_kind_of Foo, item, "Everything in @collection must be Foo."
  end
end
test "something" do
  assert_something @collection.first
end

The “something” test will fail anyway if there are no objects in the collection Foo, but the previous test makes it completely clear what the real problem is.

In short, avoid this, but if you have every reason to do so, then go ahead. And if this becomes a problem in the future, the test should be simple enough to easily transform it into something less problematic.

0
source

Source: https://habr.com/ru/post/1723770/


All Articles