Reputation:
I'm very new to RSpec, so this is quite a basic question. I'm writing something which parses a lot of data from a file, does some processing and returns a summarised result, which is all very lovely to do with Ruby.
I want to put some RSpec tests around this code but I do not want to mock up data inside the test. The files are big and complex and since I want the code to work with these files I want to work directly with some in the tests.
I have done a lot of Googling and I've been unable to find any examples of best practices for such a situation - everything I find seems to suggest mock data instead.
So, to be clear, I'd like to know the best practices for running RSpec tests against actual test data files and not against something mocked up in the test code its self. I'm perfectly happy opening files, reading them, etc., that's not the problem. What I'm really interested in is how best to structure my tests and test data directories.
Upvotes: 4
Views: 1401
Reputation: 2298
Rails has a test/fixtures
directory for structured sample data.
https://guides.rubyonrails.org/testing.html#fixtures-in-action
Upvotes: 0
Reputation: 741
What I have done in the past is to create a data folder within the specs folder to hold the files I want to read from.
I understand your motivation for wanting to use the actual files, since there are usually all sorts of little edge cases within an actual file that you may not think of on your own. My advice would be to have a test that opens the file and tests for the information that you want. But also, if that test fails, create a new test to address the particular issue within the file that you found.
For example, suppose that when trying to open the file you find that it doesn't properly handle non-UTF8 data. Create a new test with the line in question as the data used (rather than simply leaving it as part of the larger test). That way, if it ever fails again, you'll be able to pinpoint exactly where the pain point is.
Upvotes: 4