Search code examples
rubyruby-on-rails-3rspecrspec2rspec-rails

Ruby way of writing this spec in "Rspec"


I was wondering if there is any Ruby way of writing the following views spec(without using Capybara/Cucumber/Webrat helpers. Should be just in rspec or rspec-rails):

expect(rendered).to include("<input class='toggle_m' name='result_configuration[information]' type='checkbox' value='1'>")

expect(rendered).to include("<textarea class=details' disabled='disabled' name=result_configuration[info][]'></textarea>")

Thing is, I need to see if the the checkbox is checked(means the value is "1", value is set to "0" when it is unchecked) then textarea should be disabled. Any idea?

Or How would you write this expectation in a more readable way? Suggestions are most welcome.

Thanks.


Solution

  • You could try a regex, but I think your method is good enough.

    expect(rendered).should =~ /<input[^>]*name='result_configuration[information]'[^>]*value='1'[^>]*>/
    expect(rendered).should =~ /<textarea[^>]*disabled='disabled'[^>]*name=result_configuration[info][][^>]*>
    

    Limitations of this method are that if there are any checked checkboxes and any disabled textareas it will pass, to do anything more I would definitely require capybara or something to actually parse the html (regexes are not parsers)

    EDIT: Added the name= part into both regexes as a response to the comment. Only advantage of this method is that it won't break if you change the class of the elements. Unfortunately I don't know any better solution other than external gems.