I want to be sure my page does not contain certain text, I know there is an assert_text function, but, how do I test the opposite?
I am using RoR 6.0, the default test framework (I think it is Test Unit) and capybara
Rails 6 is using Minitest and Capybara by default.
assert_text comes from Capybara, or to be more precise, from Capybara::Minitest::Assertions.
As you can see in Capybara::Minitest::Assertions, there is an assertion called assert_no_text, which, I suppose, does exactly what you need.
Further details can be found here.
And this is a link to a quick explanation of the difference between Test::Unit and Minitest.
Hope this helps.
Related
In rails configuring guides, https://edgeguides.rubyonrails.org/configuring.html#configuring-rails-components, under section 3.3, there are a list of methods that can be used in config generators block. Among them are system_tests and test_framework, which are defined as follows:
system_tests defines which integration tool to use to generate system
tests. Defaults to :test_unit
test_framework defines which test framework to use. Defaults to false
and will use minitest by default.
The question is what's the difference between them, cause integration tool and test framework are both synonyms for me. Besides test_unit and minitest, based in my researchs on Google, are both test frameworks, and minitest is kind of a test_unit replacement.
You migth also have noted that in test_framework definition is written: "Defaults to false and will use minitest by default". The only left question is, what?!!!
If it defaults to false, shouldn't it mean that no test framework will be used? This assertion is completely illogical.
I agree, it is confusing how they write it.
I think when they say "system test," they are referring to the popular "integration tests" or "feature tests" which as you know, interacts with the browser. So they're saying they will use test unit which will probably use capybara driver under the hood.
I think when they say "test framework," they are saying you could use something like rspec to write your tests instead of minitest.
It will probably help if you think of integration tool as the under-the-hood driver such as capybara.
Sorry I'm on mobile app and cannot see rest of your question so I will submit then update if needed.
I want to write acceptance testing through cucumber and capybara. i have experience with rspec. I want to write integration/features test cases . I want outcome to be seen on a web browser which shows the test case running. what are the gems required for writing test cases .
If you are already familiar with RSpec, I recommend you to use RSpec with Capybara for acceptance testing. RSpec is testing framework, and Capybara is a library that helps you to test web applications by simulating how a real user would interact with your app.
Acceptance tests in RSpec with Capybara are called "feature specs" and live in /spec/features directory in Rails. Capybara comes with a built in DSL for writing descriptive acceptance tests with RSpec (feature/scenario), but you may also use traditional RSpec DSL (describe/it).
Capybara supports several drivers, and its default driver RackTest doesn't support JavaScript. So, probably, you'll want to use one of the alternative drivers (I prefer Poltergeist, but it is headless, so if you want to see result in the browser, you may use Selenium driver). In this case, you'll need to set up database_cleaner to clear your database between tests. Capybara's README contains a lot of information about its configuration and usage with RSpec, configuring driver and using database_cleaner.
You can start with this and this screencasts. But remember, that they are a little bit outdated and use traditional RSpec DSL (instead of new Capybara DSL), and use old convention, when "feature" specs were called "request" specs. Currently, by convention, "request" specs, are integration tests without capybara. So you'll need to create your capybara specs in spec/features directory, not spec/requests. And if you want to use Capybara DSL, this is easy to fix too. Just replace describe with feature, it with scenario, let with given etc. It is well documented in Capybara's README.
Hope this helps.
I got a strange problem with minitest and capybara.
I am using rails 3.2.8 and test with minitest/capybara/poltergeist. Until now every went fine. I always could test my javascript stuff.
For a new project I downloaded rails 4 to get into it a little bit. And since minitest will be the testing framework I thought it would be easy. It was not. Truth be told, I am not a hero when it comes to setting up all the stuff. I just follow Ryan Bates. After a lot of adding and removing and updating a lot of gems I decided it wasn't worth to continue to use Rails 4. I had so many issues with getting into the groove with my integration tests. All the stuff I knew did not work as expected. The axe fell when almost everything worked until I wanted to test a javascript thing. I got this error:
.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/minitest-4.6.1/lib/minitest/spec.rb:190:in `it': wrong number of arguments (2 for 1) (ArgumentError)
because of this
describe "blabla" do
it "does not do what i want it to do", js: true do
pending
end
end
It will not accept the js: true argument. Funny thing is that the describe block will accept the js: true argument.
When I went back to Rails 3.2.8, because I thought it was a Rails 4 thing, this baby followed me right into a new testsuite. I tried hard to find an answer on Google but I can't find any. My other rails 3.2.8 projects still test fine, no complains about the javascript argument. But with the new apps: no javascript testing.
I am at a loss here. I have no idea where this is coming from. Since my other 3.2.8 apps still work fine, it has probably something to do with renewed gem versions? Has anybody seen this error message? I checked the complaining minitest/spec.rb file from the error message, line 190 for several minitest versions and nothing changed in the it-method.
Please let me know if you want to see stuff (Gemfile? test_helper.rb?) if you have any clue about what might be wrong. Thanks in advance!
Casper
Minitest's spec DSL does not accept a second parameter for the it blocks. The minitest-metadata gem adds support for the second argument, and the example shows how to configure Capybara to use it. Perhaps your existing projects use minitest-metadata and configure Capybara with it, and your new projects don't?
I find that in order to thoroughly test a Rails application with Rspec I am required to write more test code than actual functional Ruby code. Call me crazy but this does not seems right. Is there a different/alternate approach (even one that is not as comprehensive as Rspec).
For unit testing I guess you won't find any replacement.
But for integration testing, you could create scenarios within your browser thanks to Selenium. See: http://seleniumhq.org/
There are many option available here are the few list
Rspec
Watir
Cucumber
factorygirl
capybara
and many more
I want both, testing Ajax Content and normal toggled content through Javascript.
My Setup is:
- Rails 3.1
- Rspec (Edge)
- Cucumber (Edge)
- Capybara (Edge)
For expample, I want to have a Form, which shows particular fields only, if a certain type of this model is selected:
Article can be an external article (url)
or an internal.
Type "externa_url" should show 2 input fields and 2 checkboxes more then Type "article",
which has a body textarea instead.
Whats the best way to implement this, also with testing?
Should it be Server sided, so that partials are loaded, if a certain article type is selected,
or with javascript, toggle the needed html?
Actually inspired by Fransico (in the comments), I write down my knowledge.
I write my own answer, it may helps others too....
First I want to mention, I answer my question with – do integration testing only.
With cucumber and selenium.
And specific javascript testing with jasmine.
But, when integration testing with cucumber (edge) rails (3.1), capybara and selenium you have to be aware of some things:
Look, that you have all your gems updated!
1) Activate your driver, if you haven't already
features/support/capybara.rb
Capybara.javascript_driver = :selenium
2) At the moment only Firefox <=4 works with webdriver selenium for rails, as I found out hardly after hours of configuring and installing each component from scratch, like rack etc.
3) Capybara itself not handles much, which serves you for klicking e.g. on lists, jquery-tokeninput especially.
3.1) I use this for selecting an Item from the tokens in the list:
When(/^I select the option containing "([^\"]*)" in the Tag List/) do |text|
find("li:contains('#{text}')").click
end
You may find this method with "locate" instead of find, don't try this, api / driver has changed to find.
Find waits automatically and check for a Ajax respond in addition of dom finding elements.
4) Add your own helper / finder / click routines for your JS / Ajax responded code,
keep in mind, it is "only" an integration test, you may want to test your JS code with yasmine or another js test framework.
For furthter information also check Screencasts from Ryan Bates (http://railscasts.com), he covers several Topics on Testing Rails; check the latest one for Javascript e.g.
Or this blog: http://openmonkey.com/2010/04/09/javascript-testing-with-cucumber-capybara/
(thnx francisco)
hope this helps someone else as well.