onActivityResult not working with fragments..how to call onActivityResult in a class which extends fragment not fragment activity.Is there any other method similar to onActivityResult in fragments?
1.
Make sure your code like this:
startActivityForResult(intent,req);
and not this:
getActivity().startActivityForResult(intent,req);
2.
Make sure implement your Fragment's onActivityResult:
super.onActivityResult(requestCode, resultCode, data);
Related
I don't actually want 'anotherFunction' to execute.
But rather when it's called inside someFunction, to have it return a specific value rather than actually executing:
// Testing this function
export function someFunction(foo, bar): string {
// want to provide a mock result here
const baz = anotherFunction(foo, bar);
// do something unrelated
}
export function anotherFunction(quz, quux): any {
// do something unrelated
}
How would you go about this with jasmine? The examples I find all assume a class and then use:
// Can't use this as the method I'd like to "mock out" is not in a class
const spy = spyOn(someClass, 'aMethod');
I'm looking for something similar to the mock function in Jest. That documentation helps communicate my question better:
"Mock functions allow you to test the links between code by erasing the actual implementation of a function, ..."
But then something similar in Jasmine.
Try something like this:
import * as helpers from './file/where/anotherFunction/is';
...
spyOn(helpers, 'anotherFunction');
Check this link out.
I'm sorry if I can't explain my question but it's hard for me.
I'm trying to send an T class to my method, but I don't have direct access to my object, so, I thought use the name for create an instance with reflections, but I don't know how to send the class T to my method.
This is th definition of my method
void RedirectToActivity<T>(bool closePrevious);
And I have this code for call it
var activity = Activator.CreateInstance("myassembly", "MainActivity");
RedirectToActivity<????>(true);
With the first line I create an Instance of my class MainActivity.
In second line I need to send MainActivity class, and receive it like a T object in RedirectToActivity, but I don't know how to achieve this.
I need some like that...
RedirectToActivity<MainActivity>(true);
But I don't have direct access to MainActivity class.
I've tried this
RedirectToActivity<activity.GetType()>(true);
RedirectToActivity<typeof(activity)>(true);
But not works.
Can you just add a generic parameter argument to your RedirectToActivity method?
void RedirectToActivity<T>(T activity, bool closePrevious)
{
// Do Stuff here
}
And then pass your activity instance in:
var activity = Activator.CreateInstance<MainActivity>();
RedirectToActivity(activity, true);
I'm very new to MVC. How do I perform a Response.Clear() from within an MVC3 aspx page?
I have tried:
Response.Clear();
ViewContext.HttpContext.Response.Clear();
Neither of them seem to function at all, in that any HTML content before the statements remain in the output. The only thing that seems to work is Response.Close(), but of course that doesn't allow me to output anything afterwards.
The reason for wanting this simply a testing/debugging exercise - I just want to be able to clear the buffered output, from inline code in the aspx page, and output something else afterwards. Similar to what one could do with web forms.
Note, I don't want to perform this within the controller, as it means re-compiling every time I make a change, as well as losing session state. The point of this is to fiddle within the aspx page to avoid re-compiling each time.
Taken from your comment, that you want to mess with your code before an Action is executed and afterwards, the most ideal solution is to use custom ActionFilters:
public interface IActionFilter
{
void OnActionExecuting(ActionExecutingContext filterContext);
void OnActionExecuted(ActionExecutedContext filterContext);
}
OnActionExecuting is used to execute code before your Controller action is called, while OnActionExecuted is used after the action method has done its job.
So you hook up a custom filter like this:
public class MyCustomFilter : IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//put your custom response and shenanigans here
...
return;
}
}
After this you can decorate your controller method with this filter:
[MyCustomFilter]
public ActionResult ListSomething()
{
/* magic happens here */
}
There's a lot you can achieve here, but I suggest some further reading into this:
http://www.dotnet-tricks.com/Tutorial/mvc/b11a280114-Understanding-ASP.NET-MVC-Filters-and-Attributes.html
http://msdn.microsoft.com/en-us/magazine/gg232768.aspx
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs
http://msdn.microsoft.com/en-us/library/gg416513(vs.98).aspx
Sidenote: If this is just for learning and debugging purposes, I'd take a look at newer mvc versions (4, 5).
In stead of focussing on Response, perhaps you should focus on what you wish to return in your controller method.
If you wish to return a full rendered View
return View("myView");
If you wish to return a PartialView
return PartialView("myPartialView");
If you wish to return a FileStream
return File(myBytes, "filename.ext");
If you wish to return a Json string,
return Json(myObject);
Try
ViewContext.HttpContext.Response.Flush();
Istead of :
ViewContext.HttpContext.Response.Clear();
Im really curious how does codeigniter achieve something like this:
$this->upload->do_upload($field_name)
it looks like method chaining, but it's not. How would the structure of this look in plain OOP?
I suppose its not as simple as..?
public function upload()
{
// stuff
return $this;
}
public function do_upload()
{
// stuff
return $foo;
}
Cheers!
When you load the library in your controller it's actually doing something like this behind the scene.
include 'system/libraries/Upload.php';
$this->upload = new CI_Upload();
Now you have "$this->upload" ready to use,
Next when you call "$this->upload->do_upload()" you're actually calling a method within the library.
On the other hand Method chaining, is just a matter of making methods returns an instance of the same object, You can review this in libraries code in CodeIgniter 3 on GitHub.
Where most library uses method chaining now.
Now when the constructor to create a response with HttpResponseMessage<T> and content is gone how should you do it if you want to test your controller? Request.CreateResponse<T> should be used instead and that is like such:
public HttpResponseMessage Post()
{
// Do Something
var response = Request.CreateResponse<SomeType>(System.Net.HttpStatusCode.OK, someObject);
return response;
}
Now to test a controller like this I have to stub the Request object since that is a property on the base class and I should use it to create the response, but that is not something I like and wonder if I can do it some other way?
From my experience, if you don't need to modify HttpResponseMessage, avoid to use it in ApiController, this will make your code simpler:
public SomeType Post()
{
// Do Something
return someObject;
}
In case it cannot be avoided, here is your answer:
ASP.NET WebApi unit testing with Request.CreateResponse
If you don't return an HttpResponseMessage then WebApi will call a MediaTypeFormatter for you to write the result to the response stream (i.e. it will use a MediaTypeFormatter to create an HttpResponseMessage for you). Many of the default implementations will do something similar to what you have above. Easiest solution is to return SomeType from your function and then utilize one of the built-in MediaTypeFormatters to do the conversion. You can test both solutions in isolation and together.