2

I'm very rigorous when it comes to my HTML markup and I follow a strict coding convention for forms, lists, etc...

I would like to include reusable test in my RSpec tests that would allow for me call a form test from any other test and target it directly to the page or URL that I'm testing.

Something like this:

# spec/helpers/form_tester.rb
describe FormTester
  it "should check to see if all the text fields have an ID prefix of 'input-'" do
    ... @form should be valid ...
    should be true
  end
end

# spec/requests/user_form.rb
describe UserForm
  it "should validate the form" do
    @form = find(:tag,'form')
    # call the FormTester method        
  end
end

Any ideas on how todo this? I'm using Rails 3.1, with RSpec, Capybara and FactoryGirl.

1
  • you should create a method containing your test code and call it from both places Nov 26, 2011 at 20:33

2 Answers 2

11

Use shared examples. In you case, something like this may work:

# spec/shared_examples_for_form.rb
shared_examples 'a form' do
  describe 'validation' do
    it 'should be validated' do
      form.should_be valid
    end
  end
end

# spec/requests/user_form.rb
describe UserForm
  it_behaves_like 'a form' do
    let(:form) { find(:tag, 'form') }
  end
end

It's also possible to pass parameters to shared examples and place the shared examples inside spec/support. Just have a read at the documentation.

1
  • 1
    Just a note: I had to move the shared_example spec-file into the spec/support directory (and then I restarted guard) in order to get the above advice to work.
    – Sky
    Feb 23, 2012 at 5:35
1

Shared examples are great, but you've got at least two serious problems here.

First: why are you giving your form fields IDs at all? You've already got perfectly good selectors: just use input[name='whatever']. And even if you are giving them IDs, don't put a prefix on them: input#whatever or just #whatever is probably a more sensible selector in your CSS than #input-whatever. By being overspecific on your selector names, you're most likely making your CSS and JavaScript harder to write than they have to be.

Second: don't use RSpec to test your views. RSpec is at its best when confined to models. Cucumber is better for anything user-facing.

8
  • not necessary Cucumber, RSpec with support of capybara/webrat can be good as well for view or integration tests
    – Artur79
    Jun 7, 2012 at 13:54
  • @Artur79 No, it can't. Cucumber is necessary, because you need to be doing your view and integration tests from the point of view of the UI -- and defining them in terms of UI actions. RSpec looks like it works for integration tests, but in fact it introduces a subtle but very real theoretical wrongness by defining your tests in programmer terms. This couples your tests too closely to implementation, and so you're not testing what you think you are. Jun 7, 2012 at 17:11
  • @MarnenLaibow-Koser: "you need to be doing your view and integration tests from the point of view of the UI -- and defining them in terms of UI actions". That's exactly what Capybara does. Cucumber is not necessary for anything. I personally don't find it as useful as rspec/capybara combo.
    – Zyren
    Jul 25, 2013 at 17:02
  • @Zyren Cucumber is necessary to prevent you from thinking like a programmer when you're writing UI tests. If you find Cucumber a hindrance, you're probably thinking at a lower level of abstraction than you should be when writing UI tests. Jul 25, 2013 at 18:26
  • @MarnenLaibow-KoserYou Capybara does exactly that. All my tests with capybara are UI actions such as visiting a URL, clicking on buttons, filling out html inputs, etc, and i can see them being done through firefox when i run my tests. Cucumber may work for you, but rspec/capybara also works and is just as capable for view/UI tests. There are many large rails projects, such as Spree, that use capybara instead of cucumber.
    – Zyren
    Jul 25, 2013 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.