xctest How to use data captured in one test method in another test method - xctest

In one test method I am calling an API and storing data to class property which I want to use in another test method as well.
But every time test method gets called, new instance of test class is allocated to it and hence class property initialises every time.
So is there any way to store the data to use it in other test methods? I don't want top use static data.

Maybe this fits to your problem...
for the first method do something like
var persistence: CoreDataPersistence?
override func setUp() {
super.setUp()
persistence = CoreDataPersistence()
}
You can also use XCTAssert... methods in the setUp method as you do in the test methods.
Then write your test methods as usual.

Related

How to pass a runtime parameter as part of the dependency resolution for a generic class?

A similar question has been answered here:
How can I pass a runtime parameter as part of the dependency resolution?
However, I was wondering how this can be done when registering a generic class?
Normally, I would register it as following:
services.AddScoped(typeof(ITest<>), typeof(Test<>));
But what if I want to pass a runtime parameter to constructor? Without using DI, it would be something like:
new Test<MyClass>(string mystring, int myInt)
In the linked answer it's suggests using a factory method but this is giving me an error if I don't pass it the exact type.
The alternative would be to get an instance without passing a runtime parameter in the constructor and instead using a setter method after getting exact instance. I would like to avoid this however because every time after getting instance you must remember to call setter method.
Is there some way around it? I guess I could use some factory class instead of registering it in startup class...
EDIT:
After reading Steven's answer which was very useful, I updated question with more concrete example:
Following example is inside some method:
//instance of repository are passed inside constructor of class
//calling some to update/insert
//IMPORTANT - calling external service I want save parameters to db no matter what
using(var ctx=new DbContext())
{
//create log object
ctx.logs.add(Obj)
ctx.save()
}
//some code after
Let's say I want to be consistent and call method of my loggingrepository and there add logging object and save everything to database
However, every repository in constructor accepts DbContext, which is registered as scoped (durig one request).
If it's inside transaction, saving depends about code after calling external service and it can throw exception and save nothing.
So yeah, I could create new dbContext and pass it in logging method or call some private logging function and save inside it,
but point is that if I would ask for instance of loggingRepository I would want DI to pass this localy created dbContext variable to constructor
and not one registered as scoped inside startup method, so that addind and saving log happens no matter what external service or code after calling it does.
My situation in something similar, but it's going for some data in db based on current user and I don't wanna pass same parameter to numerous method, but only inside class constructor.
The general solution in injecting primitive configuration values into your application components, is to extract them into a Parameter Object. This gives those values a new, unambiguous type, which can be registered into your container:
// Parameter Object
public TestConfiguration
{
public string Mystring;
public int MyInt;
}
// (Generic) class using the Parameter Object
public class Test<T>
{
public Test(TestConfiguration config) { ... }
}
// Registering both
services.AddScoped(typeof(ITest<>), typeof(Test<>));
services.AddSingleton(new TestConfiguration { Mystring = ..., Myint = ... });
Configuration values are not considered to be runtime data as their values are known at startup and constant for the duration of the application. That's why you can supply them to the constructors of your application components.
Real runtime data, however, should not be passed on to a component during construction. Runtime data are values that are not known at startup and typically passed along by the user through a web request, are retrieved from the database, session, or anything that can change during the lifetime of the application.
Instead of passing runtime data in through the constructor, you should either:
Pass runtime data through method calls of the API or
Retrieve runtime data from specific abstractions that allow resolving runtime data.
You can find more information about passing runtime data here.

Ninject - Resolve instance per method call

I'm finding a solution to resolve an instance per method call.
Something like that:
public class ServiceAPI
{
public void ServiceAction()
{
//Call certain repository action
// Ex:
Kernel.Get<RepositoryA>().Insert();
}
}
public class RepositoryA
{
public void Insert(object a)
{
//Get logger per service call ?
var logger = Kernel.Get<RepositoryA>().Insert();
}
}
I wanna the logger instance created one time per service call and it will be used throughout the repository.
I try with Ninject.Extensions.NamedScope extensions but it haven't worked yet.
Can you have any way to deal with this scenario ?
It is not possible to achieve this by using a scoping mechanism. (InCallScope(), InNamedScope(...),...).
Scoping is only relevant when ninject is calling the constructor of a type.
Ninject cannot - ever - replace the instance that is already passed to an object.
If you want to do this you have to program it yourself.
Here's two design alternatives how you can achieve what you want:
instantiate an object tree per method invocation. If there's some service infrastructure like WCF or Web-API there are probably hooks which can be used to do so.
replace the object which should be instantiated per method call by a proxy. The proxy can then use Ninject to create the target for each method call and execute the method on it.
For proxying you can use tools like Castle DynamicProxy or LinFu. There's also Ninject.Extensions.Interception which may also be helpful.

Sencha Touch: perform read (load) operation from singleton model

I have a singleton model and an associated AJAX proxy.
If I make a call to MyModel.load(), I get the error:
MyModel.load is not a function
However, you do have load in Model:
http://docs.sencha.com/touch/2.4/2.4.1-apidocs/#!/api/Ext.data.Model-static-method-load
On the contrary, MyModel.save() exists and I can access it.
Is this a bug or am I missing something?
The load method listed on Ext.data.Model is a static method on the class definition, not an instance. The documentation even denotes this is a static method. When you want to load a record, you don't load an already instantiated record, you load the model definition and that loading creates an instance.
The save method listed on Ext.data.Model is an instance method, the docs do not denote this as a static method. You don't save a class definition, you save an instance.
Example usage: https://fiddle.sencha.com/#fiddle/lvj

Best way of object instantiation as part of a collection

Let's say I'm making a task list application, and let's say that there's a Task class and a TaskList class. Now, what would be the best way to add a Task to the TaskList?
new Task(TaskList,"task name")
or
TaskList.addTask("task title")
in the second case, the TaskList would be responsible for instanciating the Task class.
I'd go for the second option.
However, there could be coupled with a third option available that allows you to extend the Task class, that is a method with the following signature
TaskList.Add(Task task);
This way you would instantiate the Task class, set all properties, and then add it to the TaskList object.
Ideally, the TaskList.addTask(string taskName) method you defined, could be a helper method that ends up internally calling the third method, and you could leave that method available if you need to support greater flexibility in setting the Task object properties.

Inject Mocking using TypeMock

I using Ioc pattern(Ninject) in my application. I want make a test case (using TypeMock) to testing Ioc pattern(Ninject). but I don't know how to mock object which creating using Ninject. Kindly let me know how can I inject mock or inject dependencies using TypeMock. Thanks
I'm not sure what you're testing but you might be interested in using Isolate.Swap
Just create a fake object and then call Swap so that the next instance created shall be a fake one - no need for Ioc:
var fakeObject = Isolate.Fake.Instace<MyObject>();
Isolate.Swap.NextInstace<MyObject>().With(fakeObject);
// Call code under test
The first object of type MyObject created after this code will be a fake object.
If you do want to use Ioc to inject your fake object you need to be able to set it to return the object created using Isolate.Fake.Instace.