开发者

Rails Functional Test - excluding method from setup

开发者 https://www.devze.com 2023-01-20 21:07 出处:网络
This should be simple but I can\'t find any examples.I have standard functional tests in my rails 3 applicatio开发者_运维技巧n (descending from ActionController::TestCase) that look like:

This should be simple but I can't find any examples. I have standard functional tests in my rails 3 applicatio开发者_运维技巧n (descending from ActionController::TestCase) that look like:

test "see if 1+1=2" do
....
end

and at the top I have a setup method:

setup do
  ......
end

How do I exclude a method? I.e.

setup :except => "see if 1+1=2" do
...
end

(above doesn't work obv)


Use shoulda and context blocks, that way you can put the tests using the setup in one context, and the tests not using them in another, or outside the other context block.


I'm curious about this too. A workaround without installing and learning any new gems would be to define a private function within your test

test "see if 1+1=2" do
  setup_situation_1
  ...
end

test "see if 1 * 1 = 1" do
  setup_situation_1
  ..
end

test "see if 1/1 = 1" do
  # no setup here
  ...
end

...

private
def setup_situation_1
  ...
end

This is the DRY-est approach I can think of without installing/learning a new gem--which may be a preferable solution for some people, especially those new to rails. However, I can't find the documentation on this setup/do method--an "except" clause for setup/do would certainly be DRY-er.

0

精彩评论

暂无评论...
验证码 换一张
取 消