How to Get the Current Test Filename from Rspec

How to get the current test filename from RSpec?

As long as you're just using this for profiling your tests to identify which files need to be improved, you should be able to toss this into your spec_helper.rb file (and remove it afterwards). I fully understand that this is not pretty/clean/elegant/acceptible in production environments and I disavow that I ever wrote it :)

config.before(:each) do |example|
path = example.metadata[:example_group][:file_path]
curr_path = config.instance_variable_get(:@curr_file_path)
if (curr_path.nil? || path != curr_path)
config.instance_variable_set(:@curr_file_path, path)
puts path
end
end

Getting the full RSpec test name from within a before(:each) block

In RSpec 2.0 you can use (I'm not sure if it is a best way but it works)

x.example.metadata[:example_group][:full_description]

As for RSpec 1.X I don't know. And that's probably what you are asking for...

What's the weird file name .rb. with rspec?

There's probably an error inside your mixpanel_tracking_spec.rb

Rspec adds a dot in the end of sentence. It's not looking for a file named mixpanel_tracking_spec.rb.

Check this snipplet:

reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.")

Rspec relevant source: https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/configuration.rb#L1925

How To test in rspec if file is deleted?

From your spec, it appears that you are mocking and stubbing correctly, but you never call check_status, so the stubs and mocks don't get used. You could change your example to something like:

it 'deletes the file' do
expect(File).to receive(:delete).with("test/status.txt")
MyModel.check_status
end

It would be better still to test this with an actual file, instead of mocks and stubs, so that it also tests that the file is in the correct location, that you have the necessary permissions, etc.

Rspec Ruby on Rails Test File System in model

Quick answer: you can stub out model methods like any others. Either stub a specific instance of a model, and then stub find or whatever to return that, or stub out any_instance to if you don't want to worry about which model is involved. Something like:

it "does something" do
foo = Foo.create! some_attributes
foo.should_receive(:some_method).and_return(whatever)
Foo.stub(:find).and_return(foo)
end

The real answer is that your code is too complicated to test effectively. Your models should not even know that a filesystem exists. That behavior should be encapsulated in other classes, which you can test independently. Your model's after_save can then just call a single method on that class, and testing whether or not that single method gets called will be a lot easier.

Your methods are also very difficult to test, because they are trying to do too much. All that conditional logic and external dependencies means you'll have to do a whole lot of mocking to get to the various bits you might want to test.

This is a big topic and a good answer is well beyond the scope of this answer. Start with the Wikipedia article on SOLID and read from there for some of the reasoning behind separating concerns into individual classes and using tiny, composed methods. To give you a ballpark idea, a method with more than one branch or more than 10 lines of code is too big; a class that is more than about 100 lines of code is too big.

RSpec: how to test file operations and file content

I would suggest using StringIO for this and making sure your SUT accepts a stream to write to instead of a filename. That way, different files or outputs can be used (more reusable), including the string IO (good for testing)

So in your test code (assuming your SUT instance is sutObject and the serializer is named writeStuffTo:

testIO = StringIO.new
sutObject.writeStuffTo testIO
testIO.string.should == "Hello, world!"

String IO behaves like an open file. So if the code already can work with a File object, it will work with StringIO.



Related Topics



Leave a reply



Submit