mstrom
mstrom

Reputation: 1743

Stubbing File.open in rspec

For some reason I am getting the following exception when running a test and I can't figure out why. All the examples I see make it seem like you don't need to provide any arguments to File.stub.open. Any help is appreciated.

Failure/Error: File.stub(:open).and_return (File.new)
ArgumentError: wrong number of arguments (0 for 1..3)

From the rspec test:

File.stub(:open).and_return (File.new)

From the code being tested:

File.open(@downloaded_content_path, "wb"){|f| f.write(response.parsed_response)}

Upvotes: 0

Views: 551

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

File.new expects arguments (from the documentation):

  • new(filename, mode="r" [, opt]) → file click to toggle source
  • new(filename [, mode [, perm]] [, opt]) → file

Opens the file named by filename according to the given mode and returns a new File object.

See IO.new for a description of mode and opt.

If a file is being created, permission bits may be given in perm. These mode and permission bits are platform dependent; on Unix systems, see open(2) and chmod(2) man pages for details.

Don't create a real file, you can create a double which expects write, or you can check out a gem to fake your file system like FakeFS

Upvotes: 2

Related Questions