I'm facing an error in Neoload while recording a UI script. The redirection is not happening, I'm getting the code 200 for the status of the page instead of 301. Please help me out.
lundi 29 janvier 2018
How to get pytest list of tests inside external python script?
I would like to obtain list of tests that can be collected by pytest in my python script. I would like to have this list in the same way that it exists in request.session.items (request is the fixture).
Testing in laravel against MySql
Hello i m testing in laravel 5.5 an api using Mysql as database and DatabaseTransactions.
Suppose that i have a model Team which may be active or inactive and i want to test getting an inactive Team.
$team = Team::inactive()->inRandomOrder()->first(); $response = $this->json('post',route('teams.show',$team->id); $reponse ->assertStatus(404);
This piece of code is problematic if there is no inactive team in my fake seeded database. So what s the best approach here ?
1)Should i make sure i seed database with an inactive team ?
2) Should i make an assertion after first line and if $team==null i make team inactive for the purpose of testing .
3)Use factory during tests and create fake data ?
The only reason i avoided 3 for the time being is that in more complex relationships with foregin keys i have to seed 3-4 tables
Capybara upload images issue
I need to test file uploads page with Capybara and Selenium.
I wrote this test:
require 'rails_helper'
describe 'Images', type: :feature do
before(:each) do
@user = create(:user)
visit '/users/sign_in'
fill_in 'sing-in-email-input', with: @user.email
fill_in 'sign-in-password-input', with: @user.password
click_button 'btn-sign-in'
visit '/categories'
click_on 'btn-new-category'
expect(current_path) == new_category_path
fill_in 'category_name', with: 'Test'
click_button 'btn-create category'
visit '/categories'
first('.fa', :visible => false).click
expect(current_path) == category_path(id: Category.last.slug.to_s)
end
it 'should allow a registered user to create image and go to it page', js: true do
click_on 'btn-upload-images'
expect(current_path) == new_image_path(id: Category.last.slug.to_s)
attach_file('image[image]',
File.join(Rails.root, '/spec/fixtures/solnce-kosmos-merkuriy.jpg'), :visible => false)
click_on('btn-upload-img')
end
end
I am not able to continue write this test and check was this image successfully uploaded because on this line:
click_on('btn-upload-img')
I got this error:
Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
ActionController::RoutingError:
No route matches [GET] "/home/Anton-S/RubymineProjects/rails_projects/gallery/spec/support/uploads/image/image/8790/solnce-kosmos-merkuriy.jpg"
# /var/lib/gems/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:36:in `call_app'
# /var/lib/gems/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `block in call'
# /var/lib/gems/2.3.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `call'
# /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call'
# /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/runtime.rb:22:in `call'
# /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/sendfile.rb:111:in `call'
# /var/lib/gems/2.3.0/gems/railties-5.1.4/lib/rails/engine.rb:522:in `call'
# /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/urlmap.rb:68:in `block in call'
# /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `each'
# /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `call'
# /var/lib/gems/2.3.0/gems/capybara-2.16.1/lib/capybara/server.rb:44:in `call'
# /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/handler/webrick.rb:86:in `service'
# ------------------
# --- Caused by: ---
# Capybara::CapybaraError:
# Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true
# /var/lib/gems/2.3.0/gems/capybara-2.16.1/lib/capybara/session.rb:145:in `raise_server_error!'
Finished in 17.15 seconds (files took 2.55 seconds to load)
133 examples, 1 failure
Failed examples:
rspec ./spec/features/images_spec.rb:20 # Images should allow a registered user to create image and go to it page
I have checked that I realy have this file
/spec/fixtures/solnce-kosmos-merkuriy.jpg
on the right place.
Here is my /config/initializers/carrierwave.rb
if Rails.env.test? || Rails.env.cucumber?
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = false
end
# make sure our uploader is auto-loaded
ImageUploader
# use different dirs when testing
CarrierWave::Uploader::Base.descendants.each do |klass|
next if klass.anonymous?
klass.class_eval do
def cache_dir
"#{Rails.root}/spec/support/uploads/tmp"
end
def store_dir
"#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
end
end
my rails_helper.rb
require 'support/factory_bot'
require 'spec_helper'
require 'shoulda/matchers'
require 'capybara/rspec'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
module DeviseRequestSpecHelpers
include Warden::Test::Helpers
def sign_in(resource_or_scope, resource = nil)
resource ||= resource_or_scope
scope = Devise::Mapping.find_scope!(resource_or_scope)
login_as(resource, scope: scope)
end
def sign_out(resource_or_scope)
scope = Devise::Mapping.find_scope!(resource_or_scope)
logout(scope)
end
end
RSpec.configure do |config|
Capybara.ignore_hidden_elements = false
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.include Devise::Test::ControllerHelpers, type: :controller
config.include DeviseRequestSpecHelpers, type: :request
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
My spec_helper.rb
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
end
My view template for this images upload page:
.row.main-new-image
= form_for @image, url: create_image_path, html: {multipart: true, method: :post, remote: true } do |form|
#new_image_error_explanation
.field
.p.text-center
%span.btn.btn-new-image{id: 'new-image-button'}
%p#p-new-image-button Choose image
\#{form.file_field :image, id: 'new-image-id'}
.field
= form.hidden_field :user_id, value: current_user.id
.actions
.p.text-center
= form.submit 'Upload', class: 'btn button-upload-img', id: 'btn-upload-img'
All my other Capybara and RSpec tests works fine.
Please help me to understand what is the reason of this issue and how to solve it and prevent in the future.
I can provide more information if it can help to solve this issue.
Automated testing - Best practice - getting test data before test suite
I have a question about getting test data for automated testing, here is my problem: I need to prepare automated testing scripts for a clothes shop. And I need to know what is the best practice for getting test data needed for the following scenario:
Scenario description: Checking if a user can correctly add a product to the basket.
Given I am on the "WOMEN'S DRESSES" page
When I add "XXX" product to the bag
Then I can see "XXX" product displayed in the basket
My question is: how to ensure that the "XXX" product is always available and what is the best practice for this? Do I have to always connect to the env database and check if the "XXX" product is available and if not then insert it into DB?
Or maybe should modify a little BDD scenario and get the list of currently available products on the "WOMEN'S DRESSES" page, chose any product, add it to the bag and check if it is correctly added to the basket? ( but in that case what to do if there are no products available for the testing env ?)
I want to have efficient and strong automated tests.
Check unique property value
I have an attribute directive that has a guid as a new property
export class FieldAuthorizationDirective
{
@Input()
fieldGuid;
}
im trying to check with a test if there are multiple elements with the same fieldGuid.
here is my try in app.component.spec but my directives list is always empty :
it('should check on unique guids', () =>
{
const directives = fixture.debugElement.queryAll(By.directive(FieldAuthorizationDirective));
directives.forEach((data) =>
{
});
});
Randomized (as in QuickCheck) versus deterministic (as in SmallCheck) property checking
I know of two approaches to property checking:
-
Randomized approach (as in QuickCheck) makes you define a generator of random values for your types, then verifies your invariants for each of a large number of randomly generated cases. For example, in case of a vector space ℤⁿ (defined, say, as
[Word]), it would generate vectors of any length and direction. -
Deterministic approach (as in SmallCheck) works the same, except that it generates valus from simple and small to more complex and extensive deterministically, covering (as I understand) a small part of domain tightly. For example, in the same case as above, it would generate zero vector, then all vectors of length <= 1, then all vectors of length <= 2, and so on, eventually covering all interesting values (such as unit vectors), for which there may conceivably be unforeseen corner cases that break an invariant.
Did I get it correct? What are the benefits and downsides of either approach? What are the preferred use cases for either? Maybe, for best results, both approaches should be combined, say, covering some moderately sized "base" with a deterministic checker and then a random sample with a randomized one?
I'm looking for some solid, motivated best practice to use daily and advance from now on.