Is there a way to skip Authorization middleware when running unit testing for a Lumen application?
At the top of the test case add the following:
<?php
use Laravel\Lumen\Testing\WithoutMiddleware;
class MyTest extends TestCase
{
use WithoutMiddleware;
...
When you run unit testing again the test case will run the application without the middleware.
Related
I would like to access the seeded database during the test
my setup:
my seeds for the test contain:
FactoryBot.create_list(:user, 10)
my User factory looks like:
factory :user { email { Faker::Internet.unique.email } }
(so there is no way for me to know their email addresses beforehands)
my goal
at one point in my tests I would love to do something like:
cy.get('body')
.should('contain', `${user.last.email}`)
is there a way to achieve that result inside a Cypress test?
The Cypress Real World App, a payment application to demonstrate real-world usage of Cypress testing methods, patterns, and workflows, demonstrates how to access seeded data via a remote command implemented in a way that is language and framework agnostic.
It is done using a Cypress Command, cy.database, which calls a Cypress Task depending upon the operation (filter or find).
The tasks issue an API request to a test API endpoint which is only exposed for the test environment.
This technique can be used to access seed data from a remote or local instance, and can be used to drive tests, as demonstrated in the notifications spec.
You could use the https://github.com/shakacode/cypress-on-rails gem to get this working.
After setting up the gem your code could look like this:
cy.appFactories([
['create_list', 'user', 9],
['create', 'user', email: 'myuser#email.com'],
])
cy.visit('/users')
cy.get('body').should('contain', 'myuser#email.com')
I'm using the Dart test package: https://pub.dartlang.org/packages/test
Often, I want to run some function before or after each tests in my test file. Does the test package provide something for this?
add a setUp(() { add your code here}) before your test() function.
There is also a tearDown() which is run after each test.
If you add the setUp function at top level in main it is run for every test, if you put it inside a group it is run for every test in that group. You can have setUp/tearDown on more than one level at the same time.
tearDown is executed in any case (like finally) no matter if the test fails or succeeds.
Recently setUpAll() and tearDownAll() was added to do some set up and tear down once before and after all tests.
In a grails project (version 2.3.7) , i have an integration test using Spock :
class SimpleIntegrationTests extends IntegrationSpec{
void "test an action from controller to database"(){...}
}
This integration test launch a batch with Spring Batch. Spring batch does not accept when a batch is launched from an existing transaction :
java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again
So i tell my integration test to run without transaction, with :
static transactional = false
Doing this, the test runs with success.
But there are other integration tests in my project, which need transactions to run correctly.
And the instruction "transactionnal = false" in my test class is not confined to this test class, but affects all other integration tests triggered after my test class.
I understood there is an alphabetically execution order for the tests. So i know that if i rename my test class to be the last test class to run, it works fine, and all my tests are successful. But i think it s not an ideal answer to the problem.
So my question is : how to define that a test class is non transactionnal, in a manner that does not affect other integration tests ?
I know this is an old question and I imagine you've found a solution already, but as a work around I think you need to explicitly specify static transactional = true on integration tests that you still want to be transactional. We've had a similar issue with our Grails 2.3.7 test suite...
I use Rspec for testing under Rails 4.
I'd like to configure Rspec so that all my unit tests automatically run without callbacks, but all my integration tests run as Rails intended -- with all the same callbacks as you get in production and development.
Is this possible?
I can think of long-winded solution that involve writing conditional logic into my actual callbacks, but not elegant solutions that involve configuring Rspec.
You can replace your callback methods with test doubles when your unit tests run, as described in https://relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations/allow-a-message-on-any-instance-of-a-class
For example, if you had the following class:
def Foo < ActiveRecord::Base
before_create :my_callback
...
end
you could stub the callback as follows:
allow_any_instance_of(Foo).to receive(:my_callback)
I would like to stub a web service proxy before all my unit tests. I could call some shared code in each unit test, but I was wondering if there is a better way.
I am using Shoulda.
Thanks
in test/test_helper you can do the following:
class ActiveSupport::TestCase
def stub_some_stuff
…
end
setup :stub_some_stuff
end
Be careful to ensure you don't just do it once by putting it outside of a setup block, doing so may result in the stub being torn down by the first test, and then all future requests just go straight through!
test/test_helper is a good place for common code - this will be injected into your TestCases