System.IO.Abstractions | Just like SystemWebAbstractions, but for SystemIO Yay for testable IO access!
kandi X-RAY | System.IO.Abstractions Summary
kandi X-RAY | System.IO.Abstractions Summary
At the core of the library is IFileSystem and FileSystem. Instead of calling methods like File.ReadAllText directly, use IFileSystem.File.ReadAllText. We have exactly the same API, except that ours is injectable and testable.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of System.IO.Abstractions
System.IO.Abstractions Key Features
System.IO.Abstractions Examples and Code Snippets
Community Discussions
Trending Discussions on System.IO.Abstractions
QUESTION
I created an extension method to add all JSON configuration files to the IConfigurationBuilder
ANSWER
Answered 2021-Dec-19 at 09:24The logic of comparing files seems alright, I don't find any outstanding problem with it, it is ok to prepend the "/" to match what you need.
Could be even better if you could use the System.IO.Path.DirectorySeparatorChar
for the directory root path as well, so if you run on windows or Linux you will have no issues.
But there may be a conceptual problem with what you are doing. To my understanding you aim to verify existence of specific configuration files required for your program to work right, if those files are missing than the program should fail. But that kind of failure due to missing configuration files, is an expected and valid result of your code. Yet, you unit-test this as if missing files should fail the test, as if missing files are an indication that something wrong with your code, this is wrong.
Missing files are not indication of your code not working correct and Unit-test should not be used as a validator to make sure the files exist prior executing the program, you will likely agree that unit-test is not part of the actual process and it should only aim to test your code and not preconditions, the test should compare an expected result (mock result of your code) vs. actual result and certainly not meant to become part of the code. That unit test looks like a validator that should be in the code.
So unless those files are produced by your specific code (and not the deployment) there is no sense testing that. In such case you need to create a configuration validator code - and your unit test could test that instead. So it will test that the validator expected result with a mock input you provide. But the thing here is that you would know that you only testing the validation logic and not the actual existence of the files.
QUESTION
I'm currently testing a class that reads an embedded json file, deserialises and loads it into my DI container. Here is a tl;dr of the code, omitting error handling:
...ANSWER
Answered 2020-Oct-08 at 15:00There is no official dependency injection package for Newtonsoft.JSON. To test your code, there is no way around creating wrappers, unless you find a mocking framework that mocks static methods.
QUESTION
So I tried to create a very simple XmlFileWriter
...ANSWER
Answered 2020-Aug-04 at 14:05StreamReader
and StreamWriter
both have constructors that accept a Stream. I recommend making your method also take streams as parameters, and the your unit tests can supply a MemoryStream
containing your test xml as a string (which can be hardcoded), while your actual application can provide a FileStream
that is the file on disk. Like so:
QUESTION
I have a Class library that does a lot of File IO. Its a bit difficult to test so I wanted to start using the System.IO.Abstractions package. It has an interface that you can implement with either the real file system, or a mocked one.
So when the code is running in production I want the real file system but when testing I want to mock it. My class doing IO stuff looks like this.
...ANSWER
Answered 2020-Jul-15 at 08:37This is your problem:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install System.IO.Abstractions
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page