dimanche 3 janvier 2021

go - Test for file created using os.Create()

I want to create a test for the following function which creates a file at a particular path:

    exists, err := IsFileExists(path)
    if err != nil {
        return err
    }
    if exists {
        return errors.New("File already exists")
    }
    file, err := os.Create(path)
    defer file.Close()
    return err

I know in JUnit there's the TemporaryFolder rule to help you set up the file and directory structure you need for your test like so:

public class DummyFileClassTest {
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void someMethod() {
        // given
        final File file1 = folder.newFile("myfile1.txt");
        final File file2 = folder.newFile("myfile2.txt");

        ... etc...
    }
}

Is there something similar in Go? Or a better approach?

Aucun commentaire:

Enregistrer un commentaire