def tiltle_finder(names)
within 'table tbody tr:nth-child(1)' do
page.should have_selector(
'td:nth-child(10)',
text: "#{names.label}"
)
end
end
it 'should find title' do
expect{
tiltle_finder(names)
}.to raise_error(Capybara::ExpectationNotMet)
but still trows that exception
Capybara::ExpectationNotMet
Instead of rescuing error you may just check that expectations are not met:
def have_title_finder(names)
within 'table tbody tr:nth-child(1)' do
have_selector(
'td:nth-child(10)',
text: "#{names.label}"
)
end
end
# ...
expect(page).not_to have_title_finder(names)
def tiltle_finder(names)
within 'table tbody tr:nth-child(1)' do
page.find('td:nth-child(10)', text: "#{names.label}")
end
end
it 'should find title' do
expect{
tiltle_finder(names)
}.to raise_error(Capybara::ElementNotFound)
end
Solved as this
Related
Does anyone know a way to skip multiple examples within a group, without duplicating the skip statement between them?
For example, given this test:
describe 'some feature' do
it 'should do something' do
...
end
it 'should do something else too' do
...
end
end
a skip doesn't work if placed before the first example, like so:
describe 'some feature' do
skip 'I would like to skip both with one statement'
it 'should do something' do
...
end
it 'should do something else too' do
...
end
end
An ideal solution would allow me to skip at any level of the example structure (describe/feature, context, and scenario/it) and would skip all children of that level of the hierarchy.
In other words, would allow me to do:
describe 'some feature' do
it 'should do something' do
...
end
it 'should do something else too' do
skip 'just one of these for now'
...
end
end
AND
describe 'some feature' do
skip 'everything within this describe block'
it 'should do something' do
...
end
it 'should do something else too' do
...
end
end
AS WELL AS
describe 'some feature' do
context 'such and such' do
skip 'just this context'
it 'should do something' do
...
end
it 'should do something else too' do
...
end
it 'but do not skip this one' do
...
end
end
As described in the documentation, you can use metadata to skip a context.
describe 'some feature', :skip do
it 'should do something' do
# This example is skipped
end
it 'should do something else too' do
# This example is skipped as well
end
end
I'm attempting to use shared_examples as a way to repeat expectations across multiple routes. In particular, I want to test whether some static assets in my header and footer are loading. However, I get an error saying that:
RSpec::Core::ExampleGroup::WrongScopeError: `it_behaves_like` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
Now, I'm not sure how to remedy this. This is my current setup.
shared_examples_for 'a page' do
describe 'should load static assets' do
it 'header, footer and icons' do
expect(page).to have_css 'footer.footer'
expect(page).to have_css '#navbar-brand'
brand = page.first(:css, '#navbar-brand')
visit brand[:src]
expect(page.status_code).to be 200
end
end
end
describe 'site pages should load static assets on requests', { :type => :feature } do
after :all do
it_behaves_like 'a page'
end
it 'home page' do
visit '/'
expect(page).to have_css 'div#main-menu a', count: 5
page.all('link[rel~="icon"]').each do |fav|
visit fav[:href]
page.status_code.should be 200
end
expect(page).to have_css 'div#main-menu'
...
end
it 'about page should have icons, footer and a header' do
visit '/about'
...
end
end
Another attempt was this:
describe 'home page' do
it_behaves_like 'a page'
end
Both fail for the same reason above. So, if I want to check the same things on every page, what is a better solution?
In RSpec 3 this should work
require 'spec_helper'
shared_examples 'a page' do
it 'has a header' do
expect(page).to have_css 'footer.footer'
end
it 'has a brand' do
expect(page).to have_css '#navbar-brand'
end
it 'points out to brand page' do
brand = page.first(:css, '#navbar-brand')
visit brand[:src]
expect(page.status_code).to be 200
end
end
describe 'home page' do
before { visit '/' }
it_behaves_like 'a page'
it 'does something else' do
# ...
end
end
Alternatively you could use a block
describe 'home page' do
it_behaves_like 'a page' do
before { visit '/' }
end
end
I use rspec like this:
describe
it 'should check if the xx':
end
How do I prevent some tests in the it end body from being run if some condition is met? For example, if the function is_disabled returns true then the following tests should not run:
it 'should check if the xx1':
end
it 'should check if the xx2':
end
but the following should:
it 'should check if the xx3':
end
it 'should check if the xx4':
end
can you do :
context "if api calls enabled for MC, #app.is_disabled => 'USD' do
it 'should check if the xx3':
end
it 'should check if the xx4':
end
end
Yes, you can use rspec implicit filters. Example:
describe "if the app is enabled", :unless => #app.is_disabled do
it 'should check if the xx3':
end
it 'should check if the xx4':
end
end
describe "if the app is disabled", :if => #app.is_disabled do
it 'should check if the xx1':
end
it 'should check if the xx2':
end
end
In the following code i want to handle the exception.if msg[0] not found i have to catch that exception message msg[2] in rescue and if it is found put the success message msg[1]
puts "Verifying Home Page"
def verifyHomepage(*args)
begin
args.each do |msg|
page.find(msg[0])
puts msg[1]
rescue
puts msg[2]
end
end
end
verifyHomepage(['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar not Found'])
In the above code iam getting
error sysntax error unexpected keyword rescue expecting keyword end
Salil has pointed you where to fix,that's correct. Now The below approach also you could adapt:
puts "Verifying Home Page"
def verifyHomepage(*args)
args.each do |msg|
next puts(msg[1]) if page.find(msg[0]) rescue nil
puts msg[2]
end
end
a = [['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar not Found']]
verifyHomepage(*a)
Output:
Verifying Home Page
Logo anchor not Found
Header Bar not Found
You have to write begin inside the block
puts "Verifying Home Page"
def verifyHomepage(*args)
args.each do |msg|
begin
page.find(msg[0])
puts msg[1]
rescue
puts msg[2]
end
end
end
verifyHomepage(['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar not Found'])
This is my specs:
it "should convert doc successfully" do
#response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.doc"))
#response[:status].should == 'ok'
File.exist?(#response[:pdf_path]).should be_true
File.exist?(#response[:swf_path]).should be_true
File.exist?(#response[:cover_path]).should be_true
end
it "should convert ppt successfully" do
#response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.ppt"))
#response[:status].should == 'ok'
File.exist?(#response[:pdf_path]).should be_true
File.exist?(#response[:swf_path]).should be_true
File.exist?(#response[:cover_path]).should be_true
end
it "should convert xls successfully" do
#response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls"))
#response[:status].should == 'ok'
File.exist?(#response[:pdf_path]).should be_true
File.exist?(#response[:swf_path]).should be_true
File.exist?(#response[:cover_path]).should be_true
end
How to merge repetition ? thanks
You could declare a custom matcher in a new conversion_helpers.rb file:
RSpec::Matchers.define :be_converted_successfully do
match do |conversion_response|
conversion_response[:status] == 'ok' && File.exist?(conversion_response[:pdf_path]) && File.exist?(conversion_response[:swf_path]) && File.exist?(conversion_response[:cover_path])
end
end
Then in your spec, require 'conversion_helpers' and you can do:
it "should convert doc successfully" do
SharpOffice::Office.process(File.expand_path("spec/fixture/test.doc")).should be_converted_successfully
end
it "should convert ppt successfully" do
SharpOffice::Office.process(File.expand_path("spec/fixture/test.ppt")).should be_converted_successfully
end
it "should convert xls successfully" do
SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls")).should be_converted_successfully
end
Although, in actual testing this could get quite annoying trying to track down a bug. But that's a different issue.
make it as a function?
put the function description in the describe block
def convert_expectation(resp)
resp[:status].should == 'ok'
File.exist?(resp[:pdf_path]).should be_true
File.exist?(resp[:swf_path]).should be_true
File.exist?(resp[:cover_path]).should be_true
end
it "should bla blabla" do
resp = SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls"))
convert_expectation(resp)
end